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
Access MySQL Field's Comments with PHP
PHP Ternary Operator Clarification
How to Remove Values from an Array in PHP
Nginx Subdirectory Root with PHP
PHP Post Limited to 1000 Variables
Difference(When Being Applied to My Code) Between Int(10) and Int(12)
How to Add Custom Fonts in Tcpdf
Defining a Namespace for Laravel 8 Routes
How to Get Average of Column Values in Laravel
Php/Apache: PHP Fatal Error: Call to Undefined Function MySQL_Connect()
How to Get the Image Type in PHP
How to Subtract 24 Hour from Date Time Object in PHP
Html2Pdf in PHP - Convert Utilities & Scripts - Examples & Demos
Wrapping a Div Around Every Third Item in a Foreach Loop PHP