【问题标题】:How to loop through mysqli_multi_query() to find out what queries succeeded?如何循环通过 mysqli_multi_query() 找出哪些查询成功了?
【发布时间】:2013-06-12 15:19:57
【问题描述】:

我正在运行多个UPDATE SQL 查询:

$queriesRun = mysqli_multi_query($connection, $queries);

现在,我如何遍历结果以了解哪些查询成功,哪些查询失败? PHP手册让我很头疼,有这么多可以在以后使用的功能。

谢谢!

【问题讨论】:

  • 如果您需要检查所有状态,为什么不将它们作为单独的查询进行呢?
  • +1 @Barmar -- 做单独的查询。 multi_query 没有任何优势,而且存在安全风险。

标签: php mysql mysqli


【解决方案1】:

我如何遍历结果以了解哪些查询成功以及 哪个失败了?

int mysqli_stmt_affected_rows ( mysqli_stmt $stmt )bool mysqli_next_result ( mysqli $link ) 是您正在寻找的 2 个函数。

<?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());
    }

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

来自documentation

如果您想使用程序样式,请查看文档中的示例。您只需使用mysqli_more_results$mysqli-&gt;next_result() 在各种查询之间切换。

【讨论】:

  • 我正在运行UPDATE,我需要知道所有更新是否成功完成。如果没有,我需要知道哪个失败了。
  • 你只是复制第一个例子吗?这可能会让 OP 头疼 ;-)
  • 也许 OP 应该告诉这是关于 UPDATE 查询以避免头痛。
  • 为什么它们是什么类型的查询会有所不同?他说他想知道他们是成功还是失败,这对于所有类型的查询都是一样的,不是吗?
  • 如果是一样的,那么答案是ok的。
【解决方案2】:

这是一个程序风格的 mysqli_multi_query 解决方案,用于接受不返回记录集的查询。它显示每个查询语句、其受影响的行以及来自 $queries 的总受影响行的运行计数。如果发生错误,mysqli_multi_query() 将停止并显示负责的错误。

$single_queries=explode(';',$queries);
if(mysqli_multi_query($connection,$queries)){
    do{
        echo "<br>",array_shift($single_queries),"<br>";
        $cumulative_rows+=$aff_rows=mysqli_affected_rows($connection);
        echo "Affected Rows = $aff_rows, ";
        echo "Cumulative Affected Rows = $cumulative_rows<br>";
    } while(mysqli_more_results($connection) && mysqli_next_result($connection));
}
if($error_mess=mysqli_error($connection)){
    echo "<br>",array_shift($single_queries),"<br>Error = $error_mess";
}

输出(假设 Column1='' 的测试表中存在 5 行):

UPDATE Test SET Column1='changed' WHERE Column1=''
Affected Rows = 5, Cumulative Affected Rows = 5

UPDATE Test SET Column1='changed again' WHERE Column1='changed'
Affected Rows = 5, Cumulative Affected Rows = 10

如果您想要更好地识别查询,请将 $queries 更改为关联数组,其中键描述每个查询,然后查看我的类似帖子: How to identify the query that caused the error using mysqli_multi_query?

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2020-04-24
    • 2017-07-02
    • 1970-01-01
    • 2011-11-27
    • 2011-08-20
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多