Home >Backend Development >PHP Tutorial >How to Resolve the \'Strict Standards: Resolving mysqli_next_result() Error\' with mysqli_multi_query?

How to Resolve the \'Strict Standards: Resolving mysqli_next_result() Error\' with mysqli_multi_query?

DDD
DDDOriginal
2024-11-28 06:29:15454browse

How to Resolve the

Strict Standards: Resolving mysqli_next_result() Error with mysqli_multi_query

Ensuring proper mysqli_multi_query utilization is crucial to avoid encountering the "Strict Standards" message. While eliminating semicolons may have eliminated an issue, it did not resolve the core problem.

To address the error message, consider the following suggestions:

Understanding mysqli_next_result()

mysqli_next_result() advances the result pointer to the next result set generated by mysqli_multi_query(). However, its execution requires an initial result set, which is not guaranteed in all cases.

Solution

Incorporate the following checks within the 'do' loop:

do { if ($result = mysqli_store_result($db)) { // process result set } } while (mysqli_more_results($db) && mysqli_next_result($db));

Alternative Syntax: WHILE{} Loop

While the classic "IF(){DO{} WHILE}" syntax is valid, a simplified and more readable alternative is:

while((isset($multi_query) && (next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli))) || (!isset($multi_query) && $multi_query = mysqli_multi_query($mysqli, implode(';', $queries))))

This approach clearly defines the conditions for executing the loop and checking for result sets.

Comprehensive Snippets

For a comprehensive solution, consider the following snippets for handling queries with or without result sets:

Classic "IF(){DO{} WHILE}" Syntax:

if (mysqli_multi_query($mysqli, implode(';', $queries))) {
    do {
        echo "<br><br>", key($queries), ": ", current($queries);  // display key:value @ pointer
        if ($result = mysqli_store_result($mysqli)) {   // if a result set
            while ($rows = mysqli_fetch_assoc($result)) {
                echo "<br>Col = {$rows["Col"]}";
            }
            mysqli_free_result($result);
        }
        echo "<br>Rows = ", mysqli_affected_rows($mysqli); // acts like num_rows on SELECTs
    } while (next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
}
if ($mysqli_error = mysqli_error($mysqli)) {
    echo "<br><br>", key($queries), ": ", current($queries), "Syntax Error:<br>$mysqli_error";  // display array pointer key:value
}

Reinvented Wheel "WHILE{}" Syntax:

while((isset($multi_query) && (next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli))) || (!isset($multi_query) && $multi_query = mysqli_multi_query($mysqli, implode(';', $queries)))) {
    echo "<br><br>", key($queries), ": ", current($queries);  // display array pointer key:value
    if ($result = mysqli_store_result($mysqli)) {
        while ($rows = mysqli_fetch_assoc($result)) {
            echo "<br>Col = {$rows["Col"]}";
        }
        mysqli_free_result($result);
    }
    echo "<br>Rows = ", mysqli_affected_rows($mysqli); // acts like num_rows on SELECTs
}
if ($mysqli_error = mysqli_error($mysqli)) {
    echo "<br><br>", key($queries), ": ", current($queries), "Syntax Error:<br>$mysqli_error";  // display array pointer key:value
}

These snippets effectively handle queries with or without result sets and provide detailed error handling for debugging purposes.

The above is the detailed content of How to Resolve the \'Strict Standards: Resolving mysqli_next_result() 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