调用多个存储过程时PHP MYSQL错误

use*_*936 5 php mysqli stored-procedures

当我在一个页面中多次调用一个过程时,我很难调用和显示内容.我试图从MYSQL的两个不同SP调用中显示两个单独的记录集.我可以显示第一个呼叫,但第二个呼叫失败.我不确定我做错了什么但也许有人可以帮忙吗?

当我调用第二个程序时,我一直收到错误:

Error calling SPCommands out of sync; you can't run this command now
Run Code Online (Sandbox Code Playgroud)

我在窗户上跑

代码如下...... PHP

// First call to SP
$page = 2;
$section = 1;

include("DatabaseConnection.php"); //general connection - works fine

$sql = 'CALL GetPageContent("'.$page.'", "'.$section.'")';

$result = mysqli_query($conn, $sql) or die('Error calling SP' .mysqli_error($conn));

while($row=mysqli_fetch_assoc($result))
{
   // DO STUFF< REMOVED TO MAKE READING CLEARER
}

mysqli_free_result($result);

//SECOND CALL BELOW


$section = 2; // change parameter for different results

$sql = 'CALL GetPageContent("'.$page.'", "'.$section.'")';

$result = mysqli_query($conn, $sql) or die('Error calling SP' .mysqli_error($conn));


while($row=mysql_fetch_assoc($result))
{
   // DO STUFF< REMOVED TO MAKE READING CLEARER
}
Run Code Online (Sandbox Code Playgroud)

Sun*_*key 9

要解决此问题,请记住在每次存储过程调用后调用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();
        ?>
Run Code Online (Sandbox Code Playgroud)