Current Location: Home> Function Categories> mysqli_result::fetch_all

mysqli_result::fetch_all

(mysqli_fetch_all) Extract all result rows into associative arrays, numeric arrays, or both
Name:mysqli_result::fetch_all
Category:MySQLi
Programming Language:php
One-line Description:Get all result rows as an associative array, an array of numbers, or both.

Definition and usage

fetch_all() / mysqli_fetch_all() functions get all result rows and return the result set as an associative array, an array of numbers, or both.

Note: This function is only available for MySQL native drivers.

Example

Example 1 - Object-Oriented Style

Get all rows and return the result set as an associative array:

 <?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname" ;
$result = $mysqli -> query ( $sql ) ;

// Get all rows
$result -> fetch_all ( MYSQLI_ASSOC ) ;

// Release the result set
$result -> free_result ( ) ;

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Get all rows and return the result set as an associative array:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

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

$sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname" ;
$result = mysqli_query ( $con , $sql ) ;

// Get all rows
mysqli_fetch_all ( $result , MYSQLI_ASSOC ) ;

// Release the result set
mysqli_free_result ( $result ) ;

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles