【问题标题】:Display the result table for 2 sql queries in php在 php 中显示 2 个 sql 查询的结果表
【发布时间】:2018-12-21 08:09:36
【问题描述】:

我正在编写一个 php 文件,我想通过执行 2 个单独的查询来显示两个表,并将它们存储在 $result 和 $result_bike 中。但是,当我尝试打开此操作表单的 html 页面时,它仅显示第一个查询的表,并在第二个位置出现错误“命令不同步;您现在无法运行此命令”桌子。

另外,我不想合并这两个表,因为它们显示完全不同的信息,我想插入一些文字来解释每个表。

我认为可能与无法为 php 打印两个表有关(我对此表示怀疑)?我应该做出什么改变?

提前感谢您的帮助!

        $result = mysql_query("CALL CrashTypeRate_Ped('$city')", $conn);
    if (!$result){
        echo "Fail to retrieve result for pedestrian crashes!\n";
        print mysql_error();
    } else {
        echo "<table border=1>\n";
        echo "<tr><td>CrashType</td><td>Count</td><td>TotalCount</td></tr>\n";
        while ($myrow = mysql_fetch_array($result)) {
            printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow["crash_type"], $myrow["type_count"], $myrow["total_count"]);
        }
        echo "</table>\n";
    }


    $result_bike = mysql_query("CALL CrashTypeRate_Bike('$city')", $conn);
    if (!$result_bike) {
        echo "Fail to retrieve result for bike crashes!\n";
        print mysql_error();
    } else {
        echo "got here!!!!!!";
        echo "<table border=1>\n";
        echo "<tr><td>CrashType</td><td>Count</td><td>TotalCount</td></tr>\n";
        while ($myrow = mysql_fetch_array($result_bike)) {
            printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow["crash_type"], $myrow["type_count"], $myrow["total_count"]);
        }
        echo "</table>\n";
    }

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这里来自 PHP 文档用户 cmets。希望这会有所帮助

    Link to PHP Documentation

    调用多个存储过程时,可能会遇到以下错误:“命令不同步;您现在无法运行此命令”。 即使在调用之间对结果对象使用 close() 函数时,也会发生这种情况。 要解决此问题,请记住在每次存储过程调用后对 mysqli 对象调用 next_result() 函数。请参见下面的示例:

    <?php
    // New Connection
    $db = new mysqli('localhost','user','pass','database');
    
    // Check for errors
    if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    }
    
    // 1st Query
    $result = $db->query("call getUsers()");
    if($result){
         // Cycle through results
        while ($row = $result->fetch_object()){
            $user_arr[] = $row;
        }
        // Free result set
        $result->close();
        $db->next_result();
    }
    
    // 2nd Query
    $result = $db->query("call getGroups()");
    if($result){
         // Cycle through results
        while ($row = $result->fetch_object()){
            $group_arr[] = $row;
        }
         // Free result set
         $result->close();
         $db->next_result();
    }
    else echo($db->error);
    
    // Close connection
    $db->close();
    ?>
    

    【讨论】:

    • 非常感谢!这确实解决了问题:)
    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    相关资源
    最近更新 更多