Example 1 — Fetch All Rows as Associative Arrays
Load a small product list at once. Sample output when connected to a demo database.
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products ORDER BY id LIMIT 2");
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($rows as $row) {
echo $row["id"], ":", $row["name"], PHP_EOL;
}
mysqli_free_result($result);
mysqli_close($mysqli); How It Works
- query() returns a mysqli_result (not a connection).
- fetch_all(. .., MYSQLI_ASSOC) builds an array of all rows.
- The foreach loop prints each id and name.
- Sample output shows two demo products.

