Created
May 31, 2014 07:30
-
-
Save abuzarhamza/a30c021290c5ef3e1355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When calling multiple stored procedures, you can run into the following error: "Commands out of sync; you can't run this command now". | |
This can happen even when using the close() function on the result object between calls. | |
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 for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment