Sophie0502
Sophie0502

Reputation: 3

Display the result table for 2 sql queries in php

I'm writing a php file, and I want to show two tables by executing 2 seperate queries, and store them in $result and $result_bike. However, when I try to open the html page for this action form, it only shows the table for the first query, and gives the error " Commands out of sync; you can't run this command now" at the place of the second table.

Also, I don't want to combine these two tables, as they show entirely different information and I want to insert some text explaining each table.

I think there might have something to do with not able to print two tables for php (which I doubt)? What change should I make?

Thank you in advance for the help!

        $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";
    }

Upvotes: 0

Views: 83

Answers (1)

Sitepose
Sitepose

Reputation: 322

Here is from PHP Documentation user comments. Hope this helps

Link to PHP Documentation

When calling multiple stored procedures, you can run into the following error: "Commands out of sync; you can't run this command now". This can happen even when using the close() function on the result object between calls. To fix the problem, remember to call the next_result() function on the mysqli object after each stored procedure call. See example below:

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

Upvotes: 1

Related Questions