English 中文(简体)
PHP MYSQL 调用多个存储程序时出错
原标题:PHP MYSQL Error when calling multiple Stored Procedures

当我在一页中多次调用一个程序时,我很难调用和显示内容。我试图显示两种不同的SP调用MISQL的两套单独记录。我可以显示第一个调用,但第二个调用失败。我不知道我做错了什么,但也许有人能帮上忙吗?

我不断得到错误 当我打电话 第二个程序:

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

我在窗边奔跑

下面的代码... 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 对象上调用 < code> 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();
?>

if you are calling more than one procedure on a single script page, you should call mysqli_next_result($connection_link) before calling another procedure. consider below sample code block:

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




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签