PHP MySQLi异步查询 [英] PHP MySQLi Asynchronous Queries with

查看:220
本文介绍了PHP MySQLi异步查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过PHP MySQLi使用异步查询.

I am trying to use asynchronous queries via PHP MySQLi.

以下代码已被简化,原始代码过于冗长,由于类依赖性以及所有这些原因而无法在此处列出.另外,请假设已经建立了对连接mysqli_handle的引用.

The following code has been simplified, the original is code is too verbose to list here because of class dependencies and all that. Also please assume the reference to the connection mysqli_handle has already been setup.

$query_1 = "SHOW TABLES FROM moxedo";
$query_2 = "CREATE TABLE `moxedo`.`mox_config_n85ad3` (`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT , `group_id` INT(3) UNSIGNED NOT NULL , `is_enabled` INT(1) UNSIGNED NOT NULL , `tag` VARCHAR(255) NOT NULL , `name` VARCHAR(80) NOT NULL , `value` VARCHAR(255) NOT NULL , `description` TEXT NOT NULL , `init_params` TEXT NOT NULL , `datetime_added` DATETIME NOT NULL , `datetime_lastmodified` DATETIME NOT NULL , `timestamp_univ` BIGINT(14) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = INNODB
";
$query_3 = "ALTER TABLE `moxedo`.`mox_config_n85ad3` ADD UNIQUE `ix_u_tag_oq` ( `tag` )";
$query_4 = "SHOW TABLES FROM moxedo";

if (!$mysqli_stmt_obj = $mysqli_handle->query($query_1)) 
{
   printf("Error: %s\n", $mysqli_handle->error);
}

if (!$mysqli_stmt_obj = $mysqli_handle->query($query_2, MYSQLI_ASYNC)) 
{
   printf("Error: %s\n", $mysqli_handle->error);
}

if (!$mysqli_stmt_obj = $mysqli_handle->query($query_3, MYSQLI_ASYNC)) 
{
   printf("Error: %s\n", $mysqli_handle->error);
}

if (!$mysqli_stmt_obj = $mysqli_handle->query($query_4)) 
{
   printf("Error: %s\n", $mysqli_handle->error);
}

对查询1的调用通过OK.对查询2的调用也通过OK.

The call to Query 1 goes through OK. The call to Query 2 also goes through OK.

但是,当我尝试执行查询3和查询4时,出现命令不同步;您不能立即运行此命令"错误.在我的在线研究中,我发现了一些有关使用mysqli_free_result的信息,但是查询2和查询3不返回结果集.

However, I'm getting "Commands out of sync; you can't run this command now" errors when I try to execute Query 3 and Query 4. From my research online I found some information on using mysqli_free_result but Query 2 and Query 3 return no resultset.

我该怎么做才能正确完成异步调用,以便我可以进行多个调用而不会出现此错误?

What do I need to do to properly finalize the asynchronous call so that I can make multiple calls without getting this error?

推荐答案

一个老问题,但是我之前遇到过这个问题,并希望在可能的情况下提供帮助.

An old question, but I ran into this before and want to help if possible.

不幸的是,尤其是在这方面缺少mysqli文档.问题是异步"模式是mysql客户端行为,而不是客户端/服务器协议的一部分.也就是说,在给定的时间,您仍然只能在连接上运行一个查询(我想是多查询). MYSQLI_ASYNC仅指定在等待查询结果时不应阻止您的应用程序.以后必须使用 mysqli_poll 收集结果.

Unfortunately, the mysqli documentation is rather lacking, particularly in this regard. The issue is that the 'async' mode is a mysql client-side behavior, and not part of the client/server protocol. That is, you can still only have one query (or multi-query, I suppose) running on a connection at a given time. MYSQLI_ASYNC only specifies that your application shouldn't block while waiting for the query results. Results have to be collected later with mysqli_poll.

在您的示例中,$ query_1是同步的,因此在返回并分配给$ mysqli_stmt_obj时已完全完成. $ query_2在$ mysqli_handle上成功异步启动,并且返回时不等待结果.到脚本到达$ query_3时,它仍具有等待$ query_2的待处理结果.因此,它尝试在完成最后一个查询之前发送另一个查询,使您命令不同步".

In your example, $query_1 is synchronous, so is completely done by the time it returns and assigns to $mysqli_stmt_obj. $query_2 is started asynchronously on $mysqli_handle successfully, and returns without waiting for the results. By the time the script gets to $query_3, it still has a pending result waiting for $query_2. Thus, it attempts to send another query before finishing the last one, giving you 'commands out of sync'.

这篇关于PHP MySQLi异步查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆