問題描述
multi_query
文檔 說:
如果第一條語句失敗,則返回 FALSE.要從其他語句中檢索后續(xù)錯(cuò)誤,您必須先調(diào)用 mysqli_next_result().
Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first.
next_result
的 文檔 說:
成功時(shí)返回 TRUE,失敗時(shí)返回 FALSE.
Returns TRUE on success or FALSE on failure.
最后,multi_query
文檔中發(fā)布的示例使用 next_result
的返回值來確定何時(shí)不再有查詢;例如停止循環(huán):
Finally, the example posted in the docs for multi_query
use the return value from next_result
to determine when there are no more queries; e.g. to stop looping:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s
", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------
");
}
} while ($mysqli->next_result()); // <-- HERE!
}
/* close connection */
$mysqli->close();
?>
我不知道提供的查詢數(shù)量,也不知道我將要執(zhí)行的 SQL.因此,我不能僅將查詢數(shù)量與返回結(jié)果的數(shù)量進(jìn)行比較.然而,如果第三個(gè)查詢是損壞的查詢,我想向用戶顯示一條錯(cuò)誤消息.但我似乎沒有辦法判斷 next_result
失敗是因?yàn)闆]有更多的查詢要執(zhí)行,還是因?yàn)?SQL 語法有錯(cuò)誤.
I don't know the number of queries provided, nor do I know anything about the SQL that I'm going to execute. I therefore can't just compare the number of queries against the number of returned results. Yet I want to display an error message to the user if, say, the third query was the broken query. But I don't seem to have a way to tell if next_result
failed because there were no more queries to execute, or if it's because there was an error in the SQL syntax.
如何檢查所有查詢是否有錯(cuò)誤?
How can I check all the queries for errors?
推薦答案
盡管文檔中有代碼示例,但也許更好的方法是這樣的:
Despite the code example in the docs, perhaps the better method would be something like this:
if ($mysqli->multi_query(...)) {
do {
// fetch results
if (!$mysqli->more_results()) {
break;
}
if (!$mysqli->next_result()) {
// report error
break;
}
} while (true);
}
這篇關(guān)于如何確保我從 MySQLi::multi_query 中捕獲到所有錯(cuò)誤?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!