Php 如何确保我捕获了MySQLi::multi_查询中的所有错误?

Php 如何确保我捕获了MySQLi::multi_查询中的所有错误?,php,mysql,mysqli,Php,Mysql,Mysqli,他们说: 如果第一条语句失败,则返回FALSE。要从其他语句中检索后续错误,必须首先调用mysqli_next_result() 他们说: 成功时返回TRUE,失败时返回FALSE 最后,文档中发布的用于multi_query的示例使用next_result的返回值来确定何时不再有查询;e、 g.停止循环: <?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connec

他们说:

如果第一条语句失败,则返回FALSE。要从其他语句中检索后续错误,必须首先调用mysqli_next_result()

他们说:

成功时返回TRUE,失败时返回FALSE

最后,文档中发布的用于
multi_query
的示例使用
next_result
的返回值来确定何时不再有查询;e、 g.停止循环:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", 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\n", $row[0]);
                }
                $result->free();
            }
            /* print divider */
            if ($mysqli->more_results()) {
                printf("-----------------\n");
            }
        } while ($mysqli->next_result()); // <-- HERE!
    }

    /* close connection */
    $mysqli->close();
    ?>

我不知道提供了多少查询,也不知道要执行的SQL。因此,我不能将查询的数量与返回结果的数量进行比较。然而,如果第三个查询是中断的查询,我想向用户显示一条错误消息。但是我似乎无法判断是因为没有更多的查询要执行,还是因为SQL语法中有错误,所以
next\u result
失败了


如何检查所有查询是否有错误?

尽管文档中有代码示例,但更好的方法可能是这样:

if ($mysqli->multi_query(...)) {
  do {
    // fetch results

    if (!$mysqli->more_results()) {
      break;
    }
    if (!$mysqli->next_result()) {
      // report error
      break;
    }
  } while (true);
}

尽管文档中有代码示例,但更好的方法可能是这样的:

if ($mysqli->multi_query(...)) {
  do {
    // fetch results

    if (!$mysqli->more_results()) {
      break;
    }
    if (!$mysqli->next_result()) {
      // report error
      break;
    }
  } while (true);
}

无法捕获所有错误,请查看您使用的示例(来自)

本例中只有“select”语句,这对于多语句脚本来说并不常见

如果您将“插入”、“更新”、“删除”或至少“设置”,则工作方式将有所不同

“multi_query”-如果第一条语句失败,则返回FALSE。第一句话——这是我们所能控制的一切。所有随后的陈述都是神秘的。如果不是“select”语句,它将在“$mysqli->store_result();”上给出一个错误,我们永远不知道它是否成功

但是

若您将SQL脚本放在“启动事务;…提交;”包装器中,那个么您可以确定—若有什么失败,一切都会失败。这很好,这有助于我们了解“一切都失败了”

要做到这一点,只需在“insert-update”脚本的末尾添加一点“select”,如果最后一条语句返回数据,则所有脚本都已成功完成

使用SQL语句,如:

START TRANSACTION;    
  set @q=1;
  select "success" as response from dual;
commit;
PHP函数:

function last_of_multi_query ($mysqli, $query) {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //After that all mysql errors will be transferred into PHP exceptions.
    $mysqli->multi_query($query);               
    do { null; } while($mysqli->next_result());
    $result = $mysqli->store_result();
    if (!$result){
        throw new Exception('multi_query failed');
    }
    return $result;
}// end function

无法捕获所有错误,请查看您使用的示例(来自)

本例中只有“select”语句,这对于多语句脚本来说并不常见

如果您将“插入”、“更新”、“删除”或至少“设置”,则工作方式将有所不同

“multi_query”-如果第一条语句失败,则返回FALSE。第一句话——这是我们所能控制的一切。所有随后的陈述都是神秘的。如果不是“select”语句,它将在“$mysqli->store_result();”上给出一个错误,我们永远不知道它是否成功

但是

若您将SQL脚本放在“启动事务;…提交;”包装器中,那个么您可以确定—若有什么失败,一切都会失败。这很好,这有助于我们了解“一切都失败了”

要做到这一点,只需在“insert-update”脚本的末尾添加一点“select”,如果最后一条语句返回数据,则所有脚本都已成功完成

使用SQL语句,如:

START TRANSACTION;    
  set @q=1;
  select "success" as response from dual;
commit;
PHP函数:

function last_of_multi_query ($mysqli, $query) {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //After that all mysql errors will be transferred into PHP exceptions.
    $mysqli->multi_query($query);               
    do { null; } while($mysqli->next_result());
    $result = $mysqli->store_result();
    if (!$result){
        throw new Exception('multi_query failed');
    }
    return $result;
}// end function

比尔·卡温的回答在我看来不是很有说服力。当do while循环已经设置为处理断点时,编写单独的条件中断似乎很奇怪

我尚未测试以下代码段,但您应该能够从中访问必要的结果和错误:

$queries["CURRENT_USER"]="SELECT CURRENT_USER()";
$queries["CITY_NAME"]="SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

if(mysqli_multi_query($mysqli,implode(';',$queries))){
    do{
        list($current_table,$current_query)=each($queries);
        if($current_table!="CURRENT_USER"){
            printf("-----------------\n");
        }
        if($result=mysqli_store_result($mysqli)){
            if(mysqli_num_rows($result)<1){
                echo "<p>Logic Error @ $current_table Query<br>$current_query</p>";
            }else{
                while($row=mysqli_fetch_row($result)){
                    printf("%s\n",$row[0]);
                }
            }
            mysqli_free_result($result);
        }
    } while(mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
}else{
    list($current_table,$current_query)=each($queries);
}
if($error=mysqli_error($mysqli)){
    echo "<p>Syntax Error @ $current_table Query<br>$current_query<br>Error: $error</p>";
}
$querys[“当前用户”]=“选择当前用户()”;
$queries[“城市名称”]=“根据ID限制从城市订单中选择名称20,5”;
if(mysqli_multi_query($mysqli,内爆(“;”,$querys))){
做{
列表($current\u table,$current\u query)=每个($querys);
if($current\u table!=“current\u USER”){
printf(“--------------\n”);
}
如果($result=mysqli\u store\u result($mysqli)){

if(mysqli_num_rows($result)Bill Karwin的答案在我看来不是很有说服力。当do-while循环已经设置为处理断点时,编写单独的条件中断似乎很奇怪

我尚未测试以下代码段,但您应该能够从中访问必要的结果和错误:

$queries["CURRENT_USER"]="SELECT CURRENT_USER()";
$queries["CITY_NAME"]="SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

if(mysqli_multi_query($mysqli,implode(';',$queries))){
    do{
        list($current_table,$current_query)=each($queries);
        if($current_table!="CURRENT_USER"){
            printf("-----------------\n");
        }
        if($result=mysqli_store_result($mysqli)){
            if(mysqli_num_rows($result)<1){
                echo "<p>Logic Error @ $current_table Query<br>$current_query</p>";
            }else{
                while($row=mysqli_fetch_row($result)){
                    printf("%s\n",$row[0]);
                }
            }
            mysqli_free_result($result);
        }
    } while(mysqli_more_results($mysqli) && mysqli_next_result($mysqli));
}else{
    list($current_table,$current_query)=each($queries);
}
if($error=mysqli_error($mysqli)){
    echo "<p>Syntax Error @ $current_table Query<br>$current_query<br>Error: $error</p>";
}
$querys[“当前用户”]=“选择当前用户()”;
$queries[“城市名称”]=“根据ID限制从城市订单中选择名称20,5”;
if(mysqli_multi_query($mysqli,内爆(“;”,$querys))){
做{
列表($current\u table,$current\u query)=每个($querys);
if($current\u table!=“current\u USER”){
printf(“--------------\n”);
}
如果($result=mysqli\u store\u result($mysqli)){

如果(mysqli_num_rows($result)我试图使用“while($mysqli->more_results()){…}”,但它变成了一个无限循环,那么我如何才能在出现错误之前获得报告错误的确切错误呢文档中说它在出错时返回false,但是当more_results()返回true但next_result()时“不返回true应该很少见。@BillKarwin我不知道您的答案如何帮助捕获MySQLi::multi_query中的所有错误,或者检查所有查询中的错误。请查看我的答案以了解详细信息。我想自从您将答案放在这里以来,问题已经改变了。我尝试使用“while($MySQLi->more_results()){…}”但是它变成了一个无限循环。我如何才能得到你所遇到的确切错误呢?
$mysqli->error
没有设置,因为
next\u result()
在遇到错误之前返回
FALSE
。next\u result()文档说它会在出错时返回FALSE,但是如果出现更多的结果()返回true,但next_result()不返回true的情况应该很少见。@BillKarwin我不知道您的答案如何帮助捕获MySQLi::multi_查询中的所有错误,或者检查所有查询是否有错误。请参阅我的answe