1. 程式人生 > >PHP+MYSQL中使用PDO獲取結果集的fetch方法

PHP+MYSQL中使用PDO獲取結果集的fetch方法

一 程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用fetch()方法獲取結果集中的下一行</title>
<style type="text/css">
<!--
body,td,th {
	font-size: 12px;
}
-->
</style></head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (檔案上傳漏洞.psd) -->
<table id="__01" width="1004" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td colspan="5">
			<img src="images/index_01.jpg" width="1004" height="182" alt=""></td>
	</tr>
	<tr>
		<td rowspan="3">
			<img src="images/index_03.jpg" width="320" height="317" alt=""></td>
		<td colspan="3">
			<img src="images/index_04.jpg" width="535" height="55" alt=""></td>
		<td rowspan="3">
			<img src="images/index_05.jpg" width="149" height="317" alt=""></td>
	</tr>
	<tr>
		<td rowspan="2">
			<img src="images/index_06.jpg" width="47" height="262" alt=""></td>
		<td width="450" height="235" align="center" valign="top" background="images/index_07.jpg">
		<table width="400" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td height="30" align="center"><strong>ID</strong></td>
            <td align="center"><strong>PDO</strong></td>
            <td align="center"><strong>資料庫</strong></td>
            <td align="center"><strong>時間</strong></td>
            <td align="center"><strong>操作</strong></td>
          </tr>
		  
	<?php
$dbms='mysql';     					//資料庫型別 ,對於開發者來說,使用不同的資料庫,只要改這個,不用記住那麼多的函式
$host='localhost'; 					//資料庫主機名
$dbName='db_database15';    		//使用的資料庫
$user='root';      					//資料庫連線使用者名稱
$pass='root';          				//對應的密碼
$dsn="$dbms:host=$host;dbname=$dbName";
try {
    $pdo = new PDO($dsn, $user, $pass); 	//初始化一個PDO物件,就是建立了資料庫連線物件$pdo
	$query="select * from tb_pdo_mysql";	//定義SQL語句
	$result=$pdo->prepare($query);			//準備查詢語句
	$result->execute();						//執行查詢語句,並返回結果集
	while($res=$result->fetch(PDO::FETCH_ASSOC)){		//while迴圈輸出查詢結果集,並且設定結果集的為關聯索引
	?>	  
          <tr>
            <td height="22" align="center" valign="middle"><?php echo $res['id'];?></td>
            <td align="center" valign="middle"><?php echo $res['pdo_type'];?></td>
            <td align="center" valign="middle"><?php echo $res['database_name'];?></td>
            <td align="center" valign="middle"><?php echo $res['dates'];?></td>
            <td align="center" valign="middle"><a href="#">刪除</a></td>
          </tr>
<?php 
	}
		  } catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
		  ?>
        </table>
		</td>
		<td rowspan="2">
			<img src="images/index_08.jpg" width="38" height="262" alt=""></td>
	</tr>
	<tr>
		<td>
			<img src="images/index_09.jpg" width="450" height="27" alt=""></td>
	</tr>
	<tr>
		<td colspan="5">
			<img src="images/index_11.jpg" width="1004" height="85" alt=""></td>
	</tr>
	<tr>
		<td colspan="5">
			<img src="images/index_12.jpg" width="1004" height="5" alt=""></td>
	</tr>
</table>
<!-- End ImageReady Slices -->
</body>
</html>
二 執行結果