mysqli_multi_query - 命令不同步;你现在不能运行这个命令 [英] mysqli_multi_query - Commands out of sync; you can't run this command now
问题描述
我有几个查询字符串,我想使用mysqli_multi_query"一次执行.这有效.
I have several query strings which I want to execute at once using "mysqli_multi_query". This works.
当我再次插入查询以使用mysqli_query"检查连接表中的每个项目时,它不会返回任何结果也来自 PHP 的任何错误.当我在 phpmyadmin 中手动运行查询字符串时,一切正常.
When I insert a query again to check each item in joined tables using "mysqli_query" it doesn't return any result nor any error from PHP. When I run the query string manually in phpmyadmin, everything works fine as it should.
这是我的代码:
<?php
$connect = mysqli_connect('localhost','root','','database');
$strquery = "";
$strquery .= "1st Query";
$strquyer .= "2nd Query";
if($multi = mysqli_multi_query($connect,$strquery)){ // function mysqli_multi_query is working
// From here it doesn't give any response
$qryarray = mysqli_query($connect,
"SELECT purchase_detail_$_SESSION[period].item_code,
purchase_detail_$_SESSION[period].location_code
FROM purchase_detail_$_SESSION[period]
WHERE purchase_detail_$_SESSION[period].purchase_num = '$_POST[purchase_num]'
UNION
SELECT purchase_detail_temp.item_code,
purchase_detail_temp.location_code
FROM purchase_detail_temp
WHERE purchase_detail_temp.purchase_num = '$_POST[purchase_num]' AND purchase_detail_temp.username = '$_SESSION[username]'");
while($array = mysqli_fetch_array($qryarray)){
"Some code to process several item code in table purchase_detail_$_SESSION[period]"
}
}
我的代码有问题吗?
推荐答案
我刚刚在 PHP 手册:
注意:如果你混合使用 $mysqli->multi_query
和 $mysqli->query
,后者不会被执行!
WATCH OUT: if you mix
$mysqli->multi_query
and$mysqli->query
, the latter(s) won't be executed!
错误代码:
$mysqli->multi_query(" Many SQL queries ; "); // OK
$mysqli->query(" SQL statement #1 ; ") // not executed!
$mysqli->query(" SQL statement #2 ; ") // not executed!
$mysqli->query(" SQL statement #3 ; ") // not executed!
$mysqli->query(" SQL statement #4 ; ") // not executed!
正确执行此操作的唯一方法是:
The only way to do this correctly is:
工作代码:
$mysqli->multi_query(" Many SQL queries ; "); // OK
while ($mysqli->next_result()) {;} // flush multi_queries
$mysqli->query(" SQL statement #1 ; ") // now executed!
$mysqli->query(" SQL statement #2 ; ") // now executed!
$mysqli->query(" SQL statement #3 ; ") // now executed!
$mysqli->query(" SQL statement #4 ; ") // now executed!
我只是在mysqli_multi_query()
之后插入这段代码:
I just insert this code after mysqli_multi_query()
:
while(mysqli_next_result($connect)){;}
这篇关于mysqli_multi_query - 命令不同步;你现在不能运行这个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!