Commands out of sync; you can’t run this command now
Mysql文档中说明错误:
Commands out of sync
If you get “Commands out of sync; you can’t run this command now” in your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.
当执行完query后,mysql将结果集放在一个result集中,产生以上问题的原因有两个:
一是在将MYSQL_RES所指对象释放(使用mysql_free_result())之前,执行了一个新的query;
二是执行了两条会返回结果的query,两者之间没有调用mysql_use_result() 或者 mysql_store_result() 来将结果集取出来。
MySQL 和客户端通信使用的是“半双工”的应答协议,客户端每发送一个查询,服务器“强行推送”结果给客户端,客户端需要执行 mysql_use_result() 或 mysql_store_result() 从服务器端取回结果,这是一次完整的查询交互,只提交查询而不取回结果就会导致 Commands out of sync。
对于不需要返回结果的查询,则通过mysql_affected_rows(_pstMql);来获取查询所影响的记录条数。
对于多线程的情况,则要保证每个线程使用独立的Mysql来对数据库进行访问,否则可能线程1还没将结果集保存起来,线程2便发起了新的查询,便会出现Commands out of sync的错误。
mysqli_query($this->conn,$sql,MYSQLI_ASYNC);
//异步会引起这个问题