Home >Backend Development >PHP Tutorial >Why Am I Getting a \'mysqli_next_result(): There is no next result set\' Error with mysqli_multi_query?

Why Am I Getting a \'mysqli_next_result(): There is no next result set\' Error with mysqli_multi_query?

Linda Hamilton
Linda HamiltonOriginal
2024-12-29 03:35:16917browse

Why Am I Getting a

Strict Standards: mysqli_next_result() error with mysqli_multi_query

Many developers encounter a strict Standards error when using mysqli_multi_query. This error message reads "mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method."

Root Cause

The error arises when mysqli_multi_query is used without checking whether there are subsequent result sets. By default, mysqli_next_result proceeds to the next result set, even if none exists. To avoid the error, the function mysqli_more_results should be invoked beforehand to ascertain the presence of any additional result sets.

Solution

To resolve the problem, use the following code:

if (mysqli_multi_query($db, $querystring)) {
    do {
        if ($result = mysqli_store_result($db)) {
            // ...
        }
    } while (mysqli_more_results($db) && mysqli_next_result($db));
}

This ensures that mysqli_next_result is only executed if there is a subsequent result set.

Additional Notes

  • It is not necessary to switch from DO WHILE to WHILE as suggested by some.
  • If no result sets are generated by the queries, no loops will be executed.
  • Additionally, the code can be modified to count the number of affected rows or display any error messages.

The above is the detailed content of Why Am I Getting a 'mysqli_next_result(): There is no next result set' Error with mysqli_multi_query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn