How to use async Mysql query with PHP PDO

倖福魔咒の 提交于 2019-12-23 09:41:08

问题


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

How to use Async queries with PDO?

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;

excepted 2 sec but result = 3 sec


回答1:


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

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



来源:https://stackoverflow.com/questions/35409298/how-to-use-async-mysql-query-with-php-pdo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!