mysqli_multi_query - Commands out of sync; you can't run this command now
I just found the answer in the PHP manual:
WATCH OUT: if you mix
$mysqli->multi_query
and$mysqli->query
, the latter(s) won't be executed!BAD CODE:
$mysqli->multi_query(" Many SQL queries ; "); // OK $mysqli->query(" SQL statement #1 ; ") // not executed! $mysqli->query(" SQL statement #2 ; ") // not executed! $mysqli->query(" SQL statement #3 ; ") // not executed! $mysqli->query(" SQL statement #4 ; ") // not executed!
The only way to do this correctly is:
WORKING CODE:
$mysqli->multi_query(" Many SQL queries ; "); // OK while ($mysqli->next_result()) {;} // flush multi_queries $mysqli->query(" SQL statement #1 ; ") // now executed! $mysqli->query(" SQL statement #2 ; ") // now executed! $mysqli->query(" SQL statement #3 ; ") // now executed! $mysqli->query(" SQL statement #4 ; ") // now executed!
I just insert this code after mysqli_multi_query()
:
while(mysqli_next_result($connect)){;}
As addition to Edison's answer:
while(mysqli_next_result($connect)){;}
I would recommend this code. This avoids exceptions.
while(mysqli_more_results($con))
{
mysqli_next_result($con);
}