PHP mysqli_result fetch_all() Method
The PHP mysqli_result::fetch_all() / mysqli_fetch_all() function is used to fetch all result rows. It returns a two-dimensional array of all result rows as an associative array, a numeric array, or both.
Note: Prior to PHP 8.1.0, available only with mysqlnd.
Syntax
//Object-oriented style public mysqli_result::fetch_all(mode) //Procedural style mysqli_fetch_all(result, mode)
Parameters
result |
Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result(). |
mode |
Optional. Indicates type of array produced from the current row data. The possible values are:
|
Return Value
Returns an array of associative or numeric arrays holding result rows.
Example: Object-oriented style
The example below shows the usage of mysqli_result::fetch_all() method.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //getting query result from the database $sql = "SELECT Name, Age FROM Employee ORDER BY Age"; $result = $mysqli->query($sql); //closing the connection $mysqli->close(); //processing the data retrieved from the database //- fetching all result rows as associative array $rows = $result->fetch_all(MYSQLI_ASSOC); //displaying the rows foreach ($rows as $row) { printf("%s, %d\n", $row["Name"], $row["Age"]); } ?>
The output of the above code will be similar to:
Marry, 23 Kim, 26 John, 27 Adam, 28
Example: Procedural style
The example below shows the usage of mysqli_fetch_all() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //getting query result from the database $sql = "SELECT Name, Age FROM Employee ORDER BY Age"; $result = mysqli_query($mysqli, $sql); //closing the connection mysqli_close($mysqli); //processing the data retrieved from the database //- fetching all result rows as associative array $rows = mysqli_fetch_all($result, MYSQLI_ASSOC); //displaying the rows foreach ($rows as $row) { printf("%s, %d\n", $row["Name"], $row["Age"]); } ?>
The output of the above code will be similar to:
Marry, 23 Kim, 26 John, 27 Adam, 28
❮ PHP MySQLi Reference