(PHP 4, PHP 5)
mysql_fetch_array — 從結果集中取得一行作為關聯數組,或數字數組,或二者兼有
說明
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);
?>
參見 mysql_fetch_row() 和 mysql_fetch_assoc()。
參數
result
resource 型的結果集。此結果集來自對 mysql_query() 的調用。
result_type
The type of array that is to be fetched. It's a constant and can
take the following values: MYSQL_ASSOC
,
MYSQL_NUM
, and
MYSQL_BOTH
.
返回值
Returns an array of strings that corresponds to the fetched row, or FALSE
if there are no more rows. The type of returned array depends on
how result_type
is defined. By using
MYSQL_BOTH
(default), you'll get an array with both
associative and number indices. Using MYSQL_ASSOC
, you
only get associative indices (as mysql_fetch_assoc()
works), using MYSQL_NUM
, you only get number indices
(as mysql_fetch_row() works).
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.
范例
Example #5 Query with aliased duplicate field names
SELECT table1.field AS foo, table2.field AS bar FROM table1, table2
Example #6 mysql_fetch_array() with 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 #7 mysql_fetch_array() with 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 #8 mysql_fetch_array() with 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);
?>
注釋
Note:
Performance
An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.
Note: 此函數返回的字段名大小寫敏感。
Note: 此函數將 NULL 字段設置為 PHP NULL
值。
參見
mysql_fetch_row() - 從結果集中取得一行作為枚舉數組 mysql_fetch_assoc() - 從結果集中取得一行作為關聯數組 mysql_data_seek() - 移動內部結果的指針 mysql_query() - 發送一條 MySQL 查詢