Commands out of sync; you can't run this command now
I think this Stack Overflow question is related to your query:

PHP Commands Out of Sync error

It's because results are not completely fetched before another call. PHP docs has a solution read this too:

http://www.php.net/manual/en/mysqli.query.php#102904