Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MYSQL Error when calling multiple Stored Procedures

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

I'm running on windows

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
}
like image 813
user1415936 Avatar asked May 24 '12 20:05

user1415936


2 Answers

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();
?>
like image 101
Sunil Kartikey Avatar answered Oct 12 '22 00:10

Sunil Kartikey


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);
like image 38
Kiran Maniya Avatar answered Oct 12 '22 00:10

Kiran Maniya