PHP Tutorial - PHP mysqli_fetch_all() Function






Definition

The mysqli_fetch_all() function fetches all result rows and returns the result-set as an associative array, a numeric array, or both.

This function is available only with MySQL Native Driver.

Syntax

mysqli_fetch_all(result,resulttype);

Parameter

ParameterIs RequiredDescription
resultRequired.Result set returned by mysqli_query(), mysqli_store_result() or mysqli_use_result()
resulttypeOptional.What type array to return.

resulttype can be one of the following values:

  • MYSQLI_ASSOC
  • MYSQLI_NUM
  • MYSQLI_BOTH




Return

It returns an array of associative or numeric arrays holding the result rows.

Example

The following code fetches all rows and return the result-set as an associative array.


<?php/* ww  w.ja  va  2 s .  c om*/
$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 FROM emp";
$result=mysqli_query($con,$sql);

// Fetch all
mysqli_fetch_all($result,MYSQLI_ASSOC);

mysqli_free_result($result);

mysqli_close($con);
?>