dongtan2603 2013-06-12 15:19
浏览 120
已采纳

如何遍历mysqli_multi_query()以找出哪些查询成功?

I'm running multiple UPDATE SQL queries as:

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

Now, how do I loop through the results to know which queries succeeded and which failed? The PHP manual is giving me a headache with so many functions that can be used afterwards.

Thanks!

  • 写回答

2条回答 默认 最新

  • duanjiu2701 2013-06-12 15:22
    关注

    how do I loop through the results to know which queries succeeded and which failed?

    int mysqli_stmt_affected_rows ( mysqli_stmt $stmt ) and bool mysqli_next_result ( mysqli $link ) are the 2 functions you're looking for.

    <?php
        $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s
    ", 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
    ", $row[0]);
                    }
                    $result->free();
                }
                /* print divider */
                if ($mysqli->more_results()) {
                    printf("-----------------
    ");
                }
            } while ($mysqli->next_result());
        }
    
        /* close connection */
        $mysqli->close();
        ?>
    

    From the documentation.

    If you wan to use procedural style, check the example in the documentation. You just have to use mysqli_more_results or $mysqli->next_result() to switch between various queries.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失