mysqli_result::fetch_all
(mysqli_fetch_all) Extract all result rows into associative arrays, numeric arrays, or both
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.
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 ( ) ; ?>
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 ) ; ?>