mysql_fetch_array() Function for PHP

What is mysql_fetch_array() function in PHP?
How does mysql_fetch_array() works?

Explanation

Mysql function mysql_fetch_array() fetches query result row as an associative array, a numeric array, or both.
Syntax
array mysql_fetch_array ( resource result [, int result_type])

Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
mysql_fetch_array function allows you to access data stored in the result returned from a successful execution of mysql_query() function.
In this function first argument is a result resource returned by mysql_query(). The optional second argument result_type in mysql_fetch_array() is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. By using MYSQL_BOTH, you will get each row of result as an array with both associative and number indices. Using MYSQL_ASSOC, you will get only associative indices, using MYSQL_NUM, you will only get number indices
After the data is retrieved, the result handles internal pointer is advanced one position. Each subsequent call to mysql_fetch_array() returns the next row in the result set. If there are no more results to return, the function returns FALSE.
If associative array data is returned, the column names are used as the keys for the array. If column names are duplicated, the data in the last column in the query sharing the name overwrites the other values. To access data from these types of queries, either use a numerically indexed array or alias the column names in your query.
Example: mysql_fetch_array with MYSQL_NUM

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
mysql_select_db("my_database",$link);
//execute simple query
$result = mysql_query("SELECT * FROM mytable",$link);
//query result is passed to mysql_fetch_array to fetch each row
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
//access array with number index
printf ("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
?>

Example: mysql_fetch_array with MYSQL_ASSOC

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
mysql_select_db("my_database",$link);
//execute simple query
$result = mysql_query("SELECT * FROM mytable",$link);
//query result is passed to mysql_fetch_array to fetch each row
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
//access array with field name as index
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}
mysql_free_result($result);
?>

Example:mysql_fetch_array() with MYSQL_BOTH

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
mysql_select_db("my_database",$link);
//execute simple query
$result = mysql_query("SELECT * FROM mytable",$link);
//query result is passed to mysql_fetch_array to fetch each row
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
//access array either with field name or number as index
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);
?>

See also: mysql_fetch_row() and mysql_fetch_assoc().

PHP Topics


Ask Questions

Ask Question