OiO.lk Blog PHP AMPHP. How to make multiple asynchronous database requests?
PHP

AMPHP. How to make multiple asynchronous database requests?


use function Amp\async;
use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlConnectionPool;


$config = MysqlConfig::fromAuthority('ip', 'user', 'psw', 'table');
$db = new MysqlConnectionPool($config);


$db->query("INSERT INTO test (str) VALUES ('start')"); 


$future = async(function($db1) {

        $db1->query("INSERT INTO test (str) VALUES ('query 1')"); 
        $db1->query("INSERT INTO test (str) VALUES ('query 2')");  // <- it doesn't work. 

}, $db);


$db->query("INSERT INTO test (str) VALUES ('finish')"); 


//$result = $future->await();  //if remove the comment, 'query 2' will be inserted

I use the AMPHP library to work with the database.

I can’t figure out why the second query doesn’t work?

Do I need to run each database query separately?

async(function($db1) {
        $db1->query("INSERT INTO test (str) VALUES ('query 1')"); 
}, $db);

async(function($db1) {
        $db1->query("INSERT INTO test (str) VALUES ('query 2')"); 
}, $db);



You need to sign in to view this answers

Exit mobile version