I have a stored procedure in my db that returns all records in a table:
CREATE PROCEDURE showAll()
BEGIN
SELECT * FROM myTable;
END
The SP works just as expected. But, if I call it in a php script and then I try to query the database again, it always fail:
// $mysqli is a db connection
// first query:
if (!$t = $mysqli->query("call showAll()"))
die('Error in the 1st query');
while ($r = $t->fetch_row()) {
echo $r[0] . "<br>"; // this is ok
}
$t->free(); // EDIT (this doesn't help anyway)
// second query (does the same thing):
if (!$t = $mysqli->query("SELECT * from myTable"))
die('Error in the 2nd query'); // I always get this error
while ($r = $t->fetch_row()) {
echo $r[0] . "<br>";
}
Notable, if I swap the two queries (i.e. I call the stored procedure at the end) it works without any error. To close() the result before the second query doesn't help. Some hints?
EDIT: mysqli::error() is: «Commands out of sync; you can't run this command now».
The comments on the php.net/manual entry for mysqli.query have been updated, and now include an answer for this. To summarize, call $mysqli->next_result() after $t->close(). Kudos to petrus.jvr!
Link: http://www.php.net/manual/en/mysqli.query.php#102904
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With