8

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
}
3
  • shouldn't the second fetch be mysqli_fetch_assoc? Commented May 24, 2012 at 20:53
  • Yes but either way I still get the same error... ? thx Commented May 24, 2012 at 20:59
  • This not work? stackoverflow.com/q/4997601/138383 Commented May 24, 2012 at 21:10

2 Answers 2

10

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();
?>
Sign up to request clarification or add additional context in comments.

2 Comments

That's great, I no longer get any errors but how do I display the following in the while loop... thank you so much for the next step ahead... I was previously calling the following in the while loop but now I get error saying: Fatal error: Cannot use object of type stdClass as array in echo "<li>\n"; echo "<a href='utilities/".$row['Image']."' rel='shadowbox[Gallery]' title='".$row['Text']."'><img src='utilities/".$row['Image']."' /></a>\n"; echo "<span class='cs-title'>".$row['Text']."</span>"; echo "</li>\n";
print $row first var_dump($row); check if it is returning what you have expected.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.