(PHP 4, PHP 5)
mysql_fetch_array — 锟接斤拷锟斤拷锟斤拷锟饺★拷锟揭伙拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷椋拷锟斤拷锟斤拷锟斤拷锟斤拷椋拷锟斤拷锟竭硷拷锟斤拷
$result
[, int $
result_type
] ) : array
锟斤拷锟截革拷锟捷从斤拷锟斤拷锟饺★拷玫锟斤拷锟斤拷锟斤拷傻锟斤拷锟斤拷椋拷锟斤拷没锟叫革拷锟斤拷锟斤拷锟津返伙拷 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
值锟斤拷