php-mysqlHow to return multiple rows as an array in MySQL using PHP?
You can return multiple rows as an array in MySQL using PHP by using the mysqli_fetch_all()
function. This function takes a result set as an argument and returns an array of associative arrays.
Example code
$result = mysqli_query($conn, "SELECT * FROM table");
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
Output example
Array
(
[0] => Array
(
[id] => 1
[name] => John
[age] => 25
)
[1] => Array
(
[id] => 2
[name] => Jane
[age] => 30
)
)
Code explanation
mysqli_query($conn, "SELECT * FROM table")
: This line executes a query to select all data from the table.mysqli_fetch_all($result, MYSQLI_ASSOC)
: This line takes the result set from the query and returns an array of associative arrays.
Helpful links
More of Php Mysql
- How to generate a UUID in MySQL using PHP?
- How to create an SSL connection to MySQL using PHP?
- How to join tables with PHP and MySQL?
- How to check the result of an insert in PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to set a timeout for MySQL query in PHP?
- How to export data from MySQL to Excel using PHP?
- How to call a stored procedure in MySQL using PHP?
- How to run multiple queries in PHP and MySQL?
- How to use the LIKE operator in PHP and MySQL?
See more codes...