more_results
In this article, we will focus on the mysqli_more_results() function in PHP, which is used to check if there are more results from a multi-query execution. We
The mysqli_more_results() function checks whether another result set is still waiting after you run several SQL statements at once with mysqli_multi_query(). It is one piece of the small toolkit you use to walk through the results of a multi-statement query. This article explains its syntax, return value, the loop pattern it belongs to, and the gotchas that trip people up.
Syntax
mysqli_more_results(mysqli $mysql): boolIn object-oriented style the same method is $mysqli->more_results().
Parameter
$mysql— a MySQLi connection object returned bymysqli_connect()(ornew mysqli(...)).
Return value
mysqli_more_results() returns true if one or more result sets are still available from the last mysqli_multi_query() call, and false if there are no more. It does not advance to that next set — for that you call mysqli_next_result().
When would I use it?
A single mysqli_query() returns one result set, so you never need mysqli_more_results() there. It only matters with mysqli_multi_query(), which lets you send several ;-separated statements in one round trip — for example a stored-procedure call that returns multiple SELECTs, or a batch import script. mysqli_more_results() is the loop condition that tells you when to stop.
The standard loop pattern
Three functions work together:
| Function | Role |
|---|---|
mysqli_more_results() | Is there another result set to come? |
mysqli_next_result() | Advance to that next result set. |
mysqli_store_result() | Grab the current result set so you can read rows. |
<?php
$mysqli = mysqli_connect("localhost", "user", "pass", "demo");
mysqli_multi_query($mysqli, "SELECT 1 AS n; SELECT 2 AS n; SELECT 3 AS n;");
do {
// Fetch the result set for the current statement.
if ($result = mysqli_store_result($mysqli)) {
$row = mysqli_fetch_assoc($result);
echo "Result: {$row['n']}\n";
mysqli_free_result($result);
}
// Keep going only while another set is queued AND we can advance to it.
} while (mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
mysqli_close($mysqli);
?>For the three statements above this prints:
Result: 1
Result: 2
Result: 3The do...while (not a plain while) is deliberate: the first result set is already current right after mysqli_multi_query(), so you must process it before testing for more.
Gotchas
- Always pair it with
mysqli_next_result().mysqli_more_results()only reports the state; it never moves the pointer. Calling it in a loop withoutmysqli_next_result()gives you an infinite loop on the first set. - Free each result set. Call
mysqli_free_result()before advancing, or you keep extra result sets in memory. - A statement that returns no rows (an
INSERT,UPDATE, or emptySELECT) makesmysqli_store_result()returnfalse— that is normal, not an error. Checkmysqli_errno($mysqli)if you need to tell the two apart.
Demonstrating the return value without a database
You don't need a live server to see what the function reports. Mirroring the logic with a small array shows the loop and the boolean clearly:
<?php
$resultSets = ["one", "two", "three"];
$index = 0;
do {
echo "Processing: {$resultSets[$index]}\n";
$index++;
// more_results() is true while another set remains.
$more = $index < count($resultSets);
echo $more ? "more_results -> true\n" : "more_results -> false\n";
} while ($more);
?>Output:
Processing: one
more_results -> true
Processing: two
more_results -> true
Processing: three
more_results -> falseRelated functions
mysqli_multi_query()— runs the multiple statements in the first place.mysqli_next_result()— advances to the next result set.mysqli_use_result()— an alternative tostore_result()that streams rows.- The MySQLi extension overview — how all these functions fit together.
Conclusion
mysqli_more_results() is the "is there more?" check in the multi-query loop. On its own it does little; combined with mysqli_next_result() and mysqli_store_result() in a do...while, it lets you cleanly process every result set returned by a single mysqli_multi_query() call.