note 102903 added to mysqli.query
| From: | petrus dot jvr at osu1 dot php dot net | Date: | Mon, 14 Mar 2011 11:38:49 +0000 |
| Subject: | note 102903 added to mysqli.query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177849@lists.php.net to get a copy of this message | ||
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:
// 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();
----
Server IP: 69.147.83.197
Probable Submitter: 41.177.93.195
----
Manual Page -- http://www.php.net/manual/en/mysqli.query.php
Edit -- https://master.php.net/note/edit/102903
Del: integrated -- https://master.php.net/note/delete/102903/integrated
Del: useless -- https://master.php.net/note/delete/102903/useless
Del: bad code -- https://master.php.net/note/delete/102903/bad+code
Del: spam -- https://master.php.net/note/delete/102903/spam
Del: non-english -- https://master.php.net/note/delete/102903/non-english
Del: in docs -- https://master.php.net/note/delete/102903/in+docs
Del: other reasons-- https://master.php.net/note/delete/102903
Reject -- https://master.php.net/note/reject/102903
Search -- https://master.php.net/manage/user-notes.php