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 a variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to change database in MySQL with PHP?
- How to use MySQL transactions in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to replace a string in MySQL using PHP?
- How to get a single value from query in PHP MySQL?
- How to get table column names in PHP MySQL?
- How to get the first row of a result in MySQL using PHP?
See more codes...