I have tried using multi_query but I keep getting a strict Standards message popping up.
$querystring = "INSERT INTO responses VALUES('1', '2', '3', '4'); INSERT INTO responses VALUES('1', '2', '3', '4')";
if (mysqli_multi_query($db, $querystring)) {
do {
if ($result = mysqli_store_result($db)) {
//
}
} while (mysqli_next_result($db));
}
echo "end";
The error message I get is:
Strict Standards: 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
I've tried adding and removing -;
but had no luck.
The reason why you get this warning, is simply because you use a
do
...while
loop that evaluates the condition after running the command block. So when there are no more results, the contents of the loop are ran one additional time, yielding that warning.Using a
while ($mysql->next_result())
...do
loop should fix this. (On a general note: Using post-test loops like you did is quite uncommon in database programming)If code is poetry, I am trying to be Shakespeare!
While pipodesign corrected the error within the $querystring and alleviated the problem, the actual solution was not provided regarding the Strict Standards error.
I disagree with SirBT's advice, changing from DO WHILE to WHILE is not necessary.
The Strict Standards message that you receive is quite informative. To obey, use this:
Then, there is no need for you to write a conditional exit or break inside of the loop because the while condition will break the loop on the first occurrence of an error. *note, the if statement before the do-while will deny entry to the loop if the first query has an error.
In your example, you are only running INSERT queries, so you won't receive any result sets to process. If you want to count how many rows you've added, use mysqli_affected_rows().
As a complete solution for your question:
Output:
LATE EDIT:
Since people new to mysqli are stumbling across this post, I'll offer a general yet robust snippet to handle queries with/without result sets using multi_query() and add a feature to display which query in the array is being handled...
Classic "IF(){DO{} WHILE}" Syntax:
Reinvented Wheel "WHILE{}" Syntax (...for those who don't like post-test loops):
So, either snippet given the following queries will offer the same output:
Query array:
Output:
Modify my snippets per your needs. Leave a comment if you discover a bug.
(Posted answer on behalf of OP).
Solved: