I'm trying to use multiple prepared statements with the mysqli driver in PHP and getting the infamous error:
Commands out of sync; you can't run this command now
Although I think this database API is atrociously brain-damaged, I do understand why I'm getting the error, as described by this question.
Several solutions are offered in this and various other answers I've found laying around, the only ones I've seen that seem to work involve
$stmt->store_result()
which buffers the entire result set.
However, I don't want to buffer the entire result set - I want to discard it without using (a large amount of) memory to store it!
All I want to do, is create a prepared statement, use bind_param()
and bind_result()
and fetch()
to get some of the results, and then somehow close the results so that I can reuse the statment later and run other queries until then.
I've tried just about every way of getting the actual result object being used from the statement and explicitly closing that, and I've tried calling next_result()
until it returns null. Neither of those solutions prevents that error from appearing.
So, how can I close a result set and execute other prepared statements after a fetch()
, without trashing the executed statement and having to re-parse the query?
$q = $dbc->prepare("SELECT id, typename, storageclass, tablename FROM _dbtype WHERE typename=?");
$q->bind_param("s", $typeName);
$q->bind_result($id,$typeName,$storageClass,$tableName);
$q->execute();
$q->fetch(); // ie. only once
... <something that does not destroy $q> ...
$q2 = $dbc->prepare("SELECT id, name FROM _storagetype");