問題描述
當運行這個腳本時,它完成得非常快(返回瀏覽器),但在瀏覽器說它完成后它仍在運行查詢.這是為什么?
When running this script it was done very quickly (returned to browser), but it was still running queries after the browser said it was done. Why is this?
推薦答案
mysqli_query 支持異步查詢.請參閱 mysqli_query
上的變更日志.mysqli_multi_query 在手冊頁上沒有特別提到異步.mysqli_multi_query
唯一要做的就是告訴 MySQL 執行一組查詢.等待結果由 PHP 決定.
mysqli_query supports async queries. See changelog on mysqli_query
. mysqli_multi_query does not mention async on the manual page specifically. Only thing mysqli_multi_query
does is tell MySQL to execute a bulk set of queries. It's up to PHP to wait for the results.
就您的代碼而言,您向 MySQL 發送大量 SQL 語句而不等待任何結果.只有當第一條語句失敗時,您的 mysqli_multi_query
才會消亡
.因此,該函數在第一條語句之后立即返回 true 并移動到下一行.這就是在 PHP 完成后執行查詢的原因.MySQL 仍在工作.PHP 已經向前發展.
As your code stands, your sending a bulk set of SQL statements to MySQL and not waiting for any results. Only time your mysqli_multi_query
will ever die
is when the first statement fails. So, that function returns true immediately after the first statement and moves on to the next line. That's why the queries are executing after the PHP is finished. MySQL is still working. PHP has moved on.
在繼續執行代碼之前,最好循環查看每個語句的結果.如果查詢在批處理中的任何地方失敗,以下內容將死亡
.
It's best that you loop through the results of each statement before moving on with your code. The following will die
if a query fails anywhere in your batch.
這篇關于mysqli_multi_query 是異步的嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!