Example 1 — Advance From First Set to Second
Read one set, call next_result, then read the next.
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");
mysqli_multi_query($mysqli, "SELECT 'first' AS label; SELECT 'second' AS label;");
$r1 = mysqli_store_result($mysqli);
$row1 = mysqli_fetch_assoc($r1);
mysqli_free_result($r1);
mysqli_next_result($mysqli);
$r2 = mysqli_store_result($mysqli);
$row2 = mysqli_fetch_assoc($r2);
mysqli_free_result($r2);
echo $row1["label"], " -> ", $row2["label"], PHP_EOL;
mysqli_close($mysqli); How It Works
- store_result loads the first SELECT.
- Free that result before moving on.
- next_result prepares the second statement's result.
- A second store_result reads "second".

