9951 explained code solutions for 126 technologies


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

Edit this code on GitHub