The mysqli_fetch_assoc() function in PHP

Admin   PHP   76  2020-07-16 11:22:38

The function mysqli_fetch_assoc() will find and return a result stream of a certain MySQL query as an associative array.


Syntax: mysqli_fetch_assoc($result);


    $result is the result of the query, the result of the function: mysqli_query(), mysqli_store_result() or mysqli_use_result().

Results returned

The function returns the associative array containing the information of the result row.

For example

How to use the mysqli_fetch_assoc() function:

// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
// Associative array
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);
// Free result set