PHP Tutorial - PHP mysqli_fetch_assoc() Function






Definition

The mysqli_fetch_array() function fetches a result row as an associative array.

Syntax

mysqli_fetch_assoc(result);

Parameter

ParameterIs RequiredDescription
resultRequired.Result set returned by mysqli_query(), mysqli_store_result() or mysqli_use_result()

Return

It returns an associative array of strings representing the fetched row. NULL if there are no more rows in result-set.





Example

The following code fetches a result row as an associative array.


<?php// ww w .  j  a v  a2s .  c o m
$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT name,salary FROM emp";
$result=mysqli_query($con,$sql);

$row=mysqli_fetch_assoc($result);
print $row["name"];
print "\n";
print $row["salary"];

mysqli_free_result($result);

mysqli_close($con);
?>