PHP mysql_fetch_assoc() Function

Definition and Usage

The mysql_fetch_assoc() function retrieves a row from the result set as an associative array.

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

syntax

mysql_fetch_assoc(data)
Parameter Description
data Required. The data pointer to be used. This data pointer is from the result returned by mysql_query().

Tips and Notes

Note:mysql_fetch_assoc() is exactly the same as using mysql_fetch_array() with the second optional parameter MYSQL_ASSOC. It only returns an associative array. This is also the initial behavior of mysql_fetch_array().

Tip:If a numeric index is needed outside the associated index, use mysql_fetch_array().

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_assoc($result));
mysql_close($con);
?>

Output:

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