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

前端 未结 3 1248
再見小時候
再見小時候 2020-12-19 03:03

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.

Er         


        
相关标签:
3条回答
  • 2020-12-19 03:46

    This helped me to remove 'Commands out of sync' error:

    do { 
        $mysqli_conn_obj->use_result(); 
    }while( $mysqli_conn_obj->more_results() && $mysqli_conn_obj->next_result() );
    

    Add this code just after calling multi_query, it will use the result sets and resolve the error.

    0 讨论(0)
  • 2020-12-19 03:55

    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.

    0 讨论(0)
  • 2020-12-19 04:01

    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));
    
    0 讨论(0)
提交回复
热议问题