7

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

当我调用第二个程序时,我不断收到错误消息:

Error calling SPCommands out of sync; you can't run this command now

我在windows上运行

下面的代码... 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
}
4

2 回答 2

10

要解决此问题,请记住next_result()在每次存储过程调用后调用 mysqli 对象上的函数。请参见下面的示例:

<?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();
?>
于 2012-05-24T21:22:16.943 回答
4

如果您在单个脚本页面上调用多个过程,则应mysqli_next_result($connection_link)在调用另一个过程之前调用。考虑下面的示例代码块:

<?php
    $data = new \stdClass();
    require('./controller/dbController.php');
    
    $query = 'CALL getActiveProductCount()';
    $result = mysqli_query($con, $query);
    $result = mysqli_fetch_assoc($result);
    $data->active_product = $result['count'];
    mysqli_next_result($con);  // required to perform before calling the procedure again

    $query = 'CALL getPurchasedProductCount()';
    $result = mysqli_query($con, $query);
    $result = mysqli_fetch_assoc($result);
    $data->purchased = $result['count'];
    mysqli_next_result($con);
于 2018-04-25T11:51:04.300 回答