Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

php - "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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By using mysqli::multi_query you are firing off queries, but you need to handle the results of those queries before you move on. The documentation page describes the various ways of handling the results. Once handled, you will be able to perform other queries fine.

The error message you are encountering is actually explained a little better on the page for mysqli::query (although bear in mind that mysqli::query would not return a result object in this instance, as you are doing a delete).

You could of course change multi_query to multiple single queries, I don't know what the pros/cons of each approach are.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...