Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Commands out of sync; you can't run this command now" - Caused by mysqli::multi_query

I am running multiple deletes through mysqli::multi_query and it is messing up the next query in line. The following error is being thrown.

Error - SQLSTATE HY000. 
Sql error: Commands out of sync; you can't run this command now

Do I need to somehow clear the multi query so it doesn't mess with my next query? What is the cause of this error?

And this is how I am running my multi query:

function deleteSomeTables($args) {
    $sql = 'delete 1;delete another;';
    if ($database->multi_query($sql)) {
        return true;
    } else {
        return false;
    }
}

I am using a recent version of Xampp on windows 7

like image 277
andrew Avatar asked Jan 26 '11 03:01

andrew


People also ask

How to execute two sql queries in php?

Multiple statements or multi queries must be executed with mysqli::multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.

Which MySqli function is used to execute queries?

The query() / mysqli_query() function performs a query against a database.

What is MySqli query?

MySQLi (MySQL Improved) provides procedural and object oriented interface to data and its management. The i extension MySQL functions allows the user to access its database servers.


1 Answers

Seva's code will most likely fix your issue, if you're using procedural style like me, use this

do {
  if ($result = mysqli_store_result($connect)) {
    mysqli_free_result($result);
  }
} while (mysqli_next_result($connect));
like image 130
Whip Avatar answered Sep 21 '22 01:09

Whip