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 use utf8mb4_unicode_ci in MySQL with PHP?
- How to use a variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to change database in MySQL with PHP?
- How to get the version of MySQL using PHP?
- How to use a MySQL union in PHP?
- How to generate a UUID in MySQL using PHP?
- How to join tables with PHP and MySQL?
- How to get table column names in PHP MySQL?
- How to create a connection pool in a MySQL database using PHP?
See more codes...