PHP mysql_fetch_array() Function

Definition and Usage

The mysql_fetch_array() function retrieves a row from the result set as an associative array, a numeric array, or both.

Returns an array generated from the rows obtained from the result set, or false if there are no more rows.

Syntax

mysql_fetch_array(data,array_type)
Parameter Description
data Optional. Specifies the data pointer to be used. This data pointer is generated by the mysql_query() function.
array_type

Optional. Specifies the type of result to return. Possible values:

  • MYSQL_ASSOC - Associative array
  • MYSQL_NUM - Numeric array
  • MYSQL_BOTH - Default. Generates both association and numeric arrays.

Tips and Comments

Note:mysql_fetch_array() is mysql_fetch_row() the extended version. In addition to storing data in the array with numeric indexing, it can also store data with associated indexing, using field names as the key names.

Tip:An important point to note is that mysql_fetch_array() is not obviously better than using mysql_fetch_row() Slow and also obviously provides more values.

Note:The field names returned by this function are case-sensitive.

Example

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='Adams'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));
mysql_close($con);
?>

Output similar to:

Array
(
[0] => Adams
[LastName] => Adams
[1] => John
[FirstName] => John
[2] => London
[City] => London
)