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

前端 未结 21 2552
庸人自扰
庸人自扰 2020-11-21 11:35

I am trying to execute my PHP code, which calls two MySQL queries via mysqli, and get the error \"Commands out of sync; you can\'t run this command now\".

Here is th

相关标签:
21条回答
  • 2020-11-21 11:59

    Another cause: store_result() cannot be called twice.

    For instance, in the following code, Error 5 is printed.

    <?php
    
    $db = new mysqli("localhost", "something", "something", "something");
    
    $stmt = $db->stmt_init();
    if ($stmt->error) printf("Error 1 : %s\n", $stmt->error);
    
    $stmt->prepare("select 1");
    if ($stmt->error) printf("Error 2 : %s\n", $stmt->error);
    
    $stmt->execute();
    if ($stmt->error) printf("Error 3 : %s\n", $stmt->error);
    
    $stmt->store_result();
    if ($stmt->error) printf("Error 4 : %s\n", $stmt->error);
    
    $stmt->store_result();
    if ($stmt->error) printf("Error 5 : %s\n", $stmt->error);
    

    (This may not be relevant to the original sample code, but it can be relevant to people seeking answers to this error.)

    0 讨论(0)
  • 2020-11-21 11:59

    Here is what was MY PROBLEM!!!

    The param binding was "dynamic" so I had a variable that sets the params of the data in order to use bind_param. So that variable was wrong but instead of throwing an error like "wrong param data" it says "out of sync bla bla bla" so I was confused...

    0 讨论(0)
  • 2020-11-21 11:59

    I ran into this error using Doctrine DBAL QueryBuilder.

    I created a query with QueryBuilder that uses column subselects, also created with QueryBuilder. The subselects were only created via $queryBuilder->getSQL() and not executed. The error happened on creating the second subselect. By provisionally executing each subselect with $queryBuilder->execute() before using $queryBuilder->getSQL(), everything worked. It is as if the connection $queryBuilder->connection remains in an invalid state for creating a new SQL before executing the currently prepared SQL, despite the new QueryBuilder instance on each subselect.

    My solution was to write the subselects without QueryBuilder.

    0 讨论(0)
  • 2020-11-21 12:01

    If you either use Buffered or Unbuffered result set for fetching data, first you must simply clear the fetched data from the memory, once you have fetched all the data. As you can't execute another MYSQL procedure on the same connection till you clear the fetched memory. Add this below function right end of your script, so it will solve the problem

    $numRecords->close(); or $numRecords->free(); // This clears the referencing memory, and will be ready for the next MYSQL fetch
    

    Reference from the PHP documentation

    0 讨论(0)
  • 2020-11-21 12:02

    You can't have two simultaneous queries because mysqli uses unbuffered queries by default (for prepared statements; it's the opposite for vanilla mysql_query). You can either fetch the first one into an array and loop through that, or tell mysqli to buffer the queries (using $stmt->store_result()).

    See here for details.

    0 讨论(0)
  • 2020-11-21 12:04

    I had today the same problem, but only when working with a stored procedure. This make the query behave like a multi query, so you need to "consume" other results available before make another query.

    while($this->mysql->more_results()){
        $this->mysql->next_result();
        $this->mysql->use_result();
    }
    
    0 讨论(0)
提交回复
热议问题