問題描述
我曾嘗試使用 multi_query,但我不斷收到嚴格的標準消息.
I have tried using multi_query but I keep getting a strict Standards message popping up.
$querystring = "INSERT INTO responses VALUES('1', '2', '3', '4'); INSERT INTO responses VALUES('1', '2', '3', '4')";
if (mysqli_multi_query($db, $querystring)) {
do {
if ($result = mysqli_store_result($db)) {
//
}
} while (mysqli_next_result($db));
}
echo "end";
我得到的錯誤信息是:
嚴格標準:mysqli_next_result():沒有下一個結果集.請調(diào)用mysqli_more_results()/mysqli::more_results()檢查是否調(diào)用這個函數(shù)/方法
Strict Standards: mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method
我嘗試添加和刪除 -;
但沒有成功.
I've tried adding and removing -;
but had no luck.
推薦答案
雖然 pipodesign 更正了 $querystring 中的錯誤并緩解了問題,但并未提供有關 Strict Standards 錯誤的實際解決方案.
While pipodesign corrected the error within the $querystring and alleviated the problem, the actual solution was not provided regarding the Strict Standards error.
我不同意 SirBT 的建議,沒有必要從 DO WHILE 更改為 WHILE.
I disagree with SirBT's advice, changing from DO WHILE to WHILE is not necessary.
您收到的嚴格標準消息非常有用.要服從,請使用:
The Strict Standards message that you receive is quite informative. To obey, use this:
do{} while(mysqli_more_results($db) && mysqli_next_result($db));
然后,您無需在循環(huán)內(nèi)部編寫條件退出或中斷,因為 while 條件將在第一次出現(xiàn)錯誤時中斷循環(huán).*注意,如果第一個查詢有錯誤,則 do-while 之前的 if 語句將拒絕進入循環(huán).
Then, there is no need for you to write a conditional exit or break inside of the loop because the while condition will break the loop on the first occurrence of an error. *note, the if statement before the do-while will deny entry to the loop if the first query has an error.
在您的示例中,您只運行 INSERT 查詢,因此您不會收到任何要處理的結果集.如果要計算添加了多少行,請使用 mysqli_affected_rows().
In your example, you are only running INSERT queries, so you won't receive any result sets to process. If you want to count how many rows you've added, use mysqli_affected_rows().
作為您問題的完整解決方案:
As a complete solution for your question:
if(mysqli_multi_query($db,$querystring)){
do{
$cumulative_rows+=mysqli_affected_rows($db);
} while(mysqli_more_results($db) && mysqli_next_result($db));
}
if($error_mess=mysqli_error($db)){echo "Error: $error_mess";}
echo "Cumulative Affected Rows: $cumulative_rows";
輸出:
// if no errors
Cumulative Affected Rows: 2
// if error on second query
Error: [something]
Cumulative Affected Rows: 1
// if error on first query
Error: [something]
Cumulative Affected Rows: 0
<小時>
后期
由于剛接觸 mysqli 的人在這篇文章中遇到了困難,我將提供一個通用但強大的代碼段來使用 multi_query() 處理有/沒有結果集的查詢,并添加一個功能來顯示正在處理數(shù)組中的哪個查詢...
Since people new to mysqli are stumbling across this post, I'll offer a general yet robust snippet to handle queries with/without result sets using multi_query() and add a feature to display which query in the array is being handled...
經(jīng)典的IF(){DO{} WHILE}"語法:
if(mysqli_multi_query($mysqli,implode(';',$queries))){
do{
echo "<br><br>",key($queries),": ",current($queries); // display key:value @ pointer
if($result=mysqli_store_result($mysqli)){ // if a result set
while($rows=mysqli_fetch_assoc($result)){
echo "<br>Col = {$rows["Col"]}";
}
mysqli_free_result($result);
}
echo "<br>Rows = ",mysqli_affected_rows($mysqli); // acts like num_rows on SELECTs
} while(next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
}
if($mysqli_error=mysqli_error($mysqli)){
echo "<br><br>",key($queries),": ",current($queries),"Syntax Error:<br>$mysqli_error"; // display array pointer key:value
}
//if you want to use the snippet again...
$mysqli_error=null; // clear variables
reset($queries); // reset pointer
<小時>
重新發(fā)明的輪子WHILE{}"語法(...對于那些不喜歡測試后循環(huán)的人):
Reinvented Wheel "WHILE{}" Syntax (...for those who don't like post-test loops):
while((isset($multi_query) && (next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli))) || (!isset($multi_query) && $multi_query=mysqli_multi_query($mysqli,implode(';',$queries)))){
echo "<br><br>",key($queries),": ",current($queries); // display array pointer key:value
if($result=mysqli_store_result($mysqli)){
while($rows=mysqli_fetch_assoc($result)){
echo "<br>Col = {$rows["Col"]}";
}
mysqli_free_result($result);
}
echo "<br>Rows = ",mysqli_affected_rows($mysqli); // acts like num_rows on SELECTs
}
if($mysqli_error=mysqli_error($mysqli)){
echo "<br><br>",key($queries),": ",current($queries),"Syntax Error:<br>$mysqli_error"; // display array pointer key:value
}
//if you want to use the snippet again...
$multi_query=$mysqli_error=null; // clear variables
reset($queries); // reset pointer
因此,給出以下查詢的任一片段都將提供相同的輸出:
So, either snippet given the following queries will offer the same output:
查詢數(shù)組:
$queries[]="SELECT * FROM `TEST`";
$queries[]="INSERT INTO `TEST` (Col) VALUES ('string1'),('string2')";
$queries[]="SELECT * FROM `TEST`";
$queries[]="DELETE FROM `TEST` WHERE Col LIKE 'string%'";
輸出:
0: SELECT * FROM `TEST`
Rows = 0
1: INSERT INTO `TEST` (Col) VALUES ('string1'),('string2')
Rows = 2
2: SELECT * FROM `TEST`
Col = string1
Col = string2
Rows = 2
3: DELETE FROM `TEST` WHERE Col LIKE 'string%'
Rows = 2
根據(jù)您的需要修改我的片段.如果您發(fā)現(xiàn)錯誤,請發(fā)表評論.
Modify my snippets per your needs. Leave a comment if you discover a bug.
這篇關于嚴格的標準:mysqli_multi_query 的 mysqli_next_result() 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!