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

前端 未结 21 2520
庸人自扰
庸人自扰 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 12:04

    The problem is the MySQL client C library, which most MySQL APIs are built on. The problem is that the C library doesn't support simultaneous execution of queries, so all APIs built on top of that also do not. Even if you use unbuffered queries. This is one reason why the asynchronous MySQL API was written. It communicates directly with the MySQL server using TCP and the wire-protocol does support simultaneous queries.

    Your solution is to either modify the algorithm so you don't need to have both in progress at once, or change them to use buffered queries, which is probably one of the original reasons for their existence in the C library (the other is to provide a kind of cursor).

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

    Check to see if you are typing all the parameters correctly. It throws the same error if the amount of parameters defined and then passed to the function are different.

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

    Create two connections, use both separately

    $mysqli = new mysqli("localhost", "Admin", "dilhdk", "SMS");
    
    $conn = new mysqli("localhost", "Admin", "dilhdk", "SMS"); 
    
    0 讨论(0)
  • 2020-11-21 12:07

    I solved this problem in my C application - here's how I did it:

    1. Quoting from mysql forums:

      This error results when you terminate your query with a semicolon delimiter inside the application. While it is required to terminate a query with a semicolon delimiter when executing it from the command line or in the query browser, remove the delimiter from the query inside your application.

    2. After running my query and dealing with the results [C API: mysql_store_result()], I iterate over any further potentially pending results that occurs via multiple SQL statement execution such as two or more select statements (back to back without dealing with the results).

      The fact is that my procedures don't return multiple results but the database doesn't know that until I execute: [C API: mysql_next_result()]. I do this in a loop (for good measure) until it returns non-zero. That's when the current connection handler knows it's okay to execute another query (I cache my handlers to minimize connection overhead).

      This is the loop I use:

      for(; mysql_next_result(mysql_handler) == 0;) 
        /* do nothing */;
      

    I don't know PHP but I'm sure it has something similar.

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

    Just for reference i had this problem mixing both multi_query and query in the same code:

    $connection->multi_query($query);
    ...
    $connection->query($otherQuery);
    

    Which for what i read had unconsumed results pending, therefore causing in the mentioned error.

    I solved it with a loop consuming all the results of the multi_query:

    $connection->multi_query($query);
    while ($connection->next_result()); // <--- solves the problem
    ...
    $connection->query($otherQuery);
    

    In my case all the queries in the multi_query where inserts so i had no interest in the results themselves.

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

    I use CodeIgniter. One server OK ... this one probably older ... Anyway using

    $this->db->reconnect();
    

    Fixed it.

    0 讨论(0)
提交回复
热议问题