Current Location: Home> Latest Articles> How to tell that next_result() successfully switches to the next result set?

How to tell that next_result() successfully switches to the next result set?

gitbox 2025-05-02

When using PHP for database operations, especially when executing multiple SQL queries, the next_result() method is often required. The purpose of next_result() is to switch the object to the result set of the next query. But the question is: How to accurately determine whether next_result() successfully switches to the next result set?

Return value of next_result()

next_result() returns a boolean value:

  • Return true : indicates that the next result set is successfully switched to the next result set, or the next result set is empty (for example, UPDATE statements that do not return rows).

  • Return false : means there are no more result sets, or an error occurred during the switching process.

Therefore, the most direct way to judge is to check the return value of next_result() .

Let’s take a look at a simple example:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

// Perform multiple queries
$sql = "SELECT * FROM users; SELECT * FROM orders;";
if ($mysqli->multi_query($sql)) {
    do {
        if ($result = $mysqli->store_result()) {
            // Process the current result set
            while ($row = $result->fetch_assoc()) {
                echo "Data rows:" . json_encode($row) . "<br>";
            }
            $result->free();
        } else {
            // The current statement is not SELECT Query like,may be UPDATE、INSERT wait
            if ($mysqli->errno) {
                echo "Saving result set failed,mistake:" . $mysqli->error . "<br>";
            }
        }

        // Try to switch to the next result set
        if ($mysqli->more_results()) {
            if (!$mysqli->next_result()) {
                echo "Switching to the next result set failed,mistake:" . $mysqli->error . "<br>";
                break;
            }
        } else {
            // No more result sets
            break;
        }

    } while (true);
} else {
    echo "Perform multiple queries失败,mistake:" . $mysqli->error;
}

$mysqli->close();
?>

A safer mode

In order to avoid missing error detection, it is recommended to check whether $mysqli->errno is 0 immediately after calling next_result() . If not 0, it means that although next_result() returns true , an error occurred during the process, such as a syntax error.

A more rigorous version can be written like this:

 if ($mysqli->more_results()) {
    if (!$mysqli->next_result()) {
        // 发生了切换mistake
        error_log("Failed to switch result set:" . $mysqli->error);
        break;
    } elseif ($mysqli->errno) {
        // next_result() return true,但实际出现了mistake
        error_log("An exception occurred while switching result sets:" . $mysqli->error);
        break;
    }
}

summary

To determine whether next_result() is successful, it should be:

  1. Check whether the return value of next_result() is true .

  2. Check if $mysqli->errno is 0.

  3. Use more_results() at the right time to determine whether it is necessary to call next_result() .

Although multiple queries are very powerful, you must be careful when handling errors, otherwise hidden bugs are prone to occur.

If you want to know more about the details of multi_query() and next_result() , you can refer to the official documentation (sample URL, domain name has been replaced by gitbox.net ).