9

我有几个查询字符串,我想使用“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]"
     }
}

我的代码有什么问题吗?

4

2 回答 2

17

我刚刚在PHP 手册中找到了答案:

注意:如果你混合$mysqli->multi_queryand $mysqli->query,后者将不会被执行!

错误代码:

$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!

正确执行此操作的唯一方法是:

工作代码:

$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()

while(mysqli_next_result($connect)){;}
于 2015-01-13T16:15:08.110 回答
9

除了爱迪生的回答:

而(mysqli_next_result($connect)){;}

我会推荐这个代码。这样可以避免异常。

while(mysqli_more_results($con))
{
   mysqli_next_result($con);
}
于 2017-05-22T08:29:34.873 回答