1. array mysql_fetch_row ( resource $result )
返回根據所取得的行生成的數組,如果沒有更多行則返回 FALSE。
mysql_fetch_row() 從和指定的結果標識關聯的結果集中取得一行數據並作爲數組返回。每個結果的列儲存在一個數組的單元中,偏移量從 0 開始。
依次調用 mysql_fetch_row() 將返回結果集中的下一行,如果沒有更多行則返回 FALSE。
Note: 本函數返回的字段名是區分大小寫的。
2. array mysql_fetch_array ( resource $result [, int $ result_type ] )
返回根據從結果集取得的行生成的數組,如果沒有更多行則返回 FALSE。
mysql_fetch_array() 是 mysql_fetch_row() 的擴展版本。除了將數據以數字索引方式儲存在數組中之外,還可以將數據作爲關聯索引儲存,用字段名作爲鍵名。
如果結果中的兩個或以上的列具有相同字段名,最後一列將優先。要訪問同名的其它列,必須用該列的數字索引或給該列起個別名。對有別名的列,不能再用原來的列名訪問其內容(本例中的 'field')。
Example #1 相同字段名的查詢
select table1.field as foo, table2.field as bar from table1, table2
有一點很重要必須指出,用 mysql_fetch_array() 並不明顯 比用 mysql_fetch_row() 慢,而且還提供了明顯更多的值。
mysql_fetch_array() 中可選的第二個參數 result_type 是一個常量,可以接受以下值:MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH。本特性是 PHP 3.0.7 起新加的。本參數的默認值是 MYSQL_BOTH。
如果用了 MYSQL_BOTH,將得到一個同時包含關聯和數字索引的數組。用MYSQL_ASSOC 只得到關聯索引(如同 mysql_fetch_assoc() 那樣),用 MYSQL_NUM 只得到數字索引(如同 mysql_fetch_row() 那樣)。
Note: 本函數返回的字段名是區分大小寫的。
Example #2 mysql_fetch_array 使用 MYSQL_NUM
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
?>
Example #3 mysql_fetch_array 使用 MYSQL_ASSOC
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}
mysql_free_result($result);
?>
Example #4 mysql_fetch_array 使用 MYSQL_BOTH
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);
?>
3. array mysql_fetch_assoc ( resource $result )
返回根據從結果集取得的行生成的關聯數組,如果沒有更多行則返回 FALSE。
mysql_fetch_assoc() 和用 mysql_fetch_array() 加上第二個可選參數 MYSQL_ASSOC 完全相同。它僅僅返回關聯數組。這也是 mysql_fetch_array() 起初始的工作方式。如果在關聯索引之外還需要數字索引,用 mysql_fetch_array()。
如果結果中的兩個或以上的列具有相同字段名,最後一列將優先。要訪問同名的其它列,要麼用 mysql_fetch_row() 來取得數字索引或給該列起個別名。參見 mysql_fetch_array() 例子中有關別名說明。
有一點很重要必須指出,用 mysql_fetch_assoc() 並不明顯 比用 mysql_fetch_row() 慢,而且還提供了明顯更多的值。
Note: 本函數返回的字段名是區分大小寫的。
Example #1 擴展的mysql_fetch_assoc() 例子
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
?>