PHP Asynchronous MySQL-Query

PHP MySQLi Asynchronous Queries with

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().

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'.

How to use async Mysql query with PHP PDO

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.

Best way to call and forget about an async mysqlnd INSERT query

If your goal is to insert a record and continue executing code without worrying about the result you can use INSERT DELAYED syntax without the need for the ASYNC flag:

$link->query("INSERT DELAYED INTO `A_Table` VALUES('stuff!')" );

It returns right away and you can continue executing new querys on the same connection. We use this on our logs system.



Related Topics



Leave a reply



Submit