How to avoid MySQL "Commands out of sync" error

I have a php while loop that I need to increment through a MySQL multi query. The first time around, it writes to the database as intended. But as soon as it loops around once, I get the following Query Error:

Commands out of sync; you can't run this command now

Here's the loop containing the query:

$i = 285;
while ( $i <= 286 ) {
	$sql = "
		CREATE TEMPORARY TABLE links_temp SELECT * FROM links WHERE 
			schoolid = 0 
			AND planid >= 8000
			AND planid <= 8005;	
		UPDATE links_temp SET linkid = NULL, schoolid = {$i} WHERE 
			schoolid = 0;
		INSERT INTO links SELECT * FROM links_temp;
		DROP TEMPORARY TABLE IF EXISTS links_temp;
	";
	$result = mysqli_multi_query( $conn, $sql ) or die( "Query Error: " . mysqli_error( $conn ) );
	$i++;
}

Open in new window


What do I need to do to enable this query to loop successfully?
Jonathan GreenbergAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
"This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between."
https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html
0
Chris StanyonWebDevCommented:
When running a multi query, you need to loop through all result sets before moving on. If you don't need to retrieve anything from the DB, then you can effectively flush the result sets with calls to mysqli_next_result():

    $result = mysqli_multi_query( $conn, $sql ) or die( "Query Error: " . mysqli_error( $conn ) );
    while(mysqli_more_results($conn)) {
        mysqli_next_result($conn);
    }
    $i++;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jonathan GreenbergAuthor Commented:
Chris,

Thank you SO much - it would have taken me hours to figure that out!

Regards,
Jonathan
0
Chris StanyonWebDevCommented:
No worries Jonathan. Glad I could help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.