The mysqli_fetch_assoc() function in PHP

Admin   PHP   134  2020-08-06 18:45:43

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

Syntax

Syntax: mysqli_fetch_assoc($result);

Inside:

    $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:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);
 
// Associative array
$row=mysqli_fetch_assoc($result);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);
 
// Free result set
mysqli_free_result($result);
 
mysqli_close($con);