When executing multi-query statements using MySQLi, sometimes we need to process each result set in turn. PHP provides two functions: mysqli_more_results() and mysqli_next_result() , allowing us to traverse these results gracefully.
This article will use an actual code example to describe how to use mysqli_more_results() to determine whether there is a next result set, so as to avoid the incorrect call to mysqli_next_result() .
Suppose you have a set of SQL statements, for example:
SELECT * FROM users;
SELECT * FROM orders;
SELECT * FROM products;
You want to execute them at once and process the results of each query one by one. This situation is suitable for managing backend batch data display, data initialization and other purposes.
First we need to connect to the database and execute multiple SQL statements using multi_query :
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "
SELECT * FROM users;
SELECT * FROM orders;
SELECT * FROM products;
";
if ($mysqli->multi_query($sql)) {
do {
// Process the current result set
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_assoc()) {
print_r($row);
}
$result->free();
}
// Check if there is another result set
if ($mysqli->more_results()) {
echo "There are more result sets,Ready to continue processing...\n";
} else {
echo "All results have been processed。\n";
}
} while ($mysqli->more_results() && $mysqli->next_result());
} else {
echo "Execution failed: " . $mysqli->error;
}
$mysqli->close();
In this example, we use the following key functions:
mysqli::multi_query($query)
Send queries containing multiple SQL statements to the database.
mysqli::store_result()
Get the current result set.
mysqli::more_results()
Check if there is still a remaining result set.
mysqli::next_result()
Move to the next result set so we can continue to process.
Through the do...while loop combining mysqli_more_results() and mysqli_next_result() , we can safely process all result sets to avoid undefined behavior or errors when calling next_result() incorrectly.
Some developers may use mysqli_next_result() directly in the loop without checking if there are more results, which may lead to the following error:
// Wrong usage:Not checked more_results() Just call next_result()
while ($mysqli->next_result()) {
// Processing result sets
}
Doing so may throw a warning or an error without subsequent results. Therefore, we should always check with more_results() before calling next_result() .
mysqli_more_results() is an important tool to determine whether there are more result sets after using multi_query() . Using it with mysqli_next_result() can ensure the robustness of the program logic and avoid errors caused by out-of-bounds access to the result set.
In actual projects, such as building a report system or background data batch logic, mastering this technique can make your PHP programs more reliable and maintainable.
For more information, please visit our development documentation: https://gitbox.net/docs