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

How to Fix \'Strict Standards: mysqli_next_result() Error\' with mysqli_multi_query()?

DDD
Release: 2024-11-29 05:39:17
Original
1031 people have browsed it

How to Fix

Strict Standards: mysqli_next_result() Error with mysqli_multi_query

Issue:

Using mysqli_multi_query() triggers a "Strict Standards" error related to mysqli_next_result().

Solution:

To resolve this issue, use the following code:

do{} while(mysqli_more_results($db) && mysqli_next_result($db));
Copy after login

Explanation:

The Strict Standards error occurs because mysqli_more_results() must be called to check if mysqli_next_result() should be used. The provided code snippet ensures that mysqli_next_result() is only called if there is another result set.

Considerations:

  • In the example provided, only INSERT queries are being executed, which do not return result sets. Therefore, the loop will not execute and the error will not occur.
  • If your queries include SELECT statements or other operations that return result sets, the provided solution will prevent the error message from appearing.
  • The modified code snippet uses a do-while loop, which is post-test and does not require a conditional break or exit within the loop.

Enhancements:

For a more robust approach, consider the following code snippet:

if(mysqli_multi_query($db,$querystring)){
    do{
        $cumulative_rows+=mysqli_affected_rows($db);
    } while(mysqli_more_results($db) && mysqli_next_result($db));
}
if($error_mess=mysqli_error($db)){echo "Error: $error_mess";}
echo "Cumulative Affected Rows: $cumulative_rows";
Copy after login

This code snippet:

  • Counts the affected rows from INSERT, UPDATE, or DELETE queries.
  • Displays the cumulative affected rows.
  • Checks for errors and provides an informative error message.

Additional Notes:

For users unfamiliar with mysqli_multi_query(), here is a versatile code snippet that handles queries with or without result sets:

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
}
Copy after login

This code snippet:

  • Works for all types of queries, including SELECT, INSERT, UPDATE, and DELETE.
  • Provides a detailed output, including the query itself, affected rows or column values, and potential errors.
  • Can be easily modified for specific needs or database structures.

The above is the detailed content of How to Fix \'Strict Standards: mysqli_next_result() Error\' with mysqli_multi_query()?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template