mysqli_multi_query - 命令不同步;你现在不能运行这个命令

时间:2015-01-12 10:14:35

标签: php mysqli

我有几个查询字符串,我想使用" mysqli_multi_query"一次执行。这很有效。

当我再次插入查询以使用" mysqli_query"检查连接表中的每个项目时它不会从PHP返回任何结果任何错误。当我在phpmyadmin中手动运行查询字符串时,一切正常。

这是我的代码:

<?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]"
     }
}

我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:8)

感谢@Fred -ii-提供您的信息,以便我可以解决问题...

我刚刚在php manual =

中找到答案
WATCH OUT: if you mix $mysqli->multi_query and $mysqli->query, the latter(s) won't be executed!

BAD CODE:
$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:

WORKING CODE:
$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()之后插入此代码:

  

而(mysqli_next_result($连接)){;}

希望它有用..谢谢..

答案 1 :(得分:5)

除了爱迪生的回答:

  

而(mysqli_next_result($连接)){;}

我会推荐这段代码。这避免了例外。

while(mysqli_more_results($con))
{
   mysqli_next_result($con);
}