如何在PHP PDO中使用异步Mysql查询 [英] How to use async Mysql query with PHP PDO

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

问题描述

Mysqlnd驱动程序PHP 5.6有机会使用异步查询 http ://php.net/manual/en/mysqli.reap-async-query.php

The Mysqlnd driver PHP 5.6 have opportunity to use a Async queries http://php.net/manual/en/mysqli.reap-async-query.php

如何在PDO中使用异步查询?

How to use Async queries with PDO?

这是行不通的,代码( PHP异步mysql-query ):

it is not work, code (PHP asynchronous mysql-query):

$dbConnectionOne = new \PDO($cnn0, $conf['user'], $conf['pass']);
$dbConnectionOne->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$dbConnectionTwo =  new \PDO($cnn0, $conf['user'], $conf['pass']);
$dbConnectionTwo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$dbConnectionTwo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);


$t = time();
$synchStmt = $dbConnectionOne->prepare('SELECT sleep(2)');
$synchStmt->execute();

$asynchStmt = $dbConnectionTwo->prepare('SELECT sleep(1)');
$asynchStmt->execute();

$measurementConfiguration = array();
foreach ($synchStmt->fetchAll() as $synchStmtRow) {
   print_r($synchStmtRow);
}

while (($asynchStmtRow = $asynchStmt->fetch()) !== false) {
   print_r($asynchStmtRow);
}


$t = time() - $t;

echo 'query execute ', $t, ' sec',PHP_EOL;

除了2秒,但结果= 3秒

excepted 2 sec but result = 3 sec

推荐答案

否.您不能将Mysql异步查询与PDO一起使用. Mysqli是唯一的选择.

No. You cannot use Mysql async queries with PDO. Mysqli is the only choice.

您可以为此使用mysqli_multi_query或常规 query/poll/reap 顺序.

You can use for this either mysqli_multi_query or the regular query/poll/reap sequence.

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

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