How to use async mysql query with php PDO

Mysqlnd PHP 5.6 driver has the ability to use Async requests http://php.net/manual/en/mysqli.reap-async-query.php

How to use Async requests with PDO?

this is not work, the code ( asynchronous mysql query php ):

$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; 

save 2 seconds but result = 3 s

+5
source share
1 answer

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

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

+5
source

Source: https://habr.com/ru/post/1243054/


All Articles