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);
?>
转载于:https://blog.51cto.com/2910071/1113005