调用多个存储过程时,PHP MYSQL错误 [英] PHP MYSQL Error when calling multiple Stored Procedures

查看:55
本文介绍了调用多个存储过程时,PHP MYSQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having difficulty calling and displaying the content when I call a procedure more than once in a page. I am trying to display two separate record sets from two different SP calls for MYSQL. I can display the first call but the second fails. I'm not sure what I am doing wrong but perhaps someone can kind help?

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

I keep getting the error when I call the second procedure:

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

我正在Windows上运行

I'm running on windows

下面的代码... PHP

Code below... 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
}

推荐答案

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

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

这篇关于调用多个存储过程时,PHP MYSQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆