I have some PHP-7 code (using "mysqli"), that, of course, wants to execute several queries and to scroll through their result sets. However, I am finding that unless I close() the previous statement, the next statement ... although it seems to run ... produces no results.
(That is to say, the affected-rows count will return zero.)
I am at a loss to explain this behavior . . .
(Incidentally, the necessary function seemed to be statement- closing. Not disposing of result sets.)
= = =
I do not believe that this is a duplicate of the question cited, because that answer is clearly using mysqli_multi_query and I am not. I am performing three different, unrelated queries, in three consecutive loops.
If you are suggesting that I am "accumulating multiple result-sets" and just don't know it, but I do not believe that this is my use-case. The only references to those API-calls that I have seen, specifically make use of the multi-query call, which I am not using. But I would be delighted to learn that I am wrong here.
Code example
echo "Test: simple query.\n";
$iter = DB::query('select count(*) from address_book');
echo "Query returned " . $iter->rowCount() . " results.\n";
foreach ($iter as $v)
print_r($v);
$iter = null; // WITHOUT this, next stmt will produce no results.
echo "Test: simple query again.\n";
$iter = DB::query('select count(*) from address_book');
echo "Query returned " . $iter->rowCount() . " results.\n";
foreach ($iter as $v)
print_r($v);
$iter = null; // DITTO ...
And here are the iterator-support functions:
// Retrieve current element
public function current () {
if ($this->current == null) {
$this->current = $this->fetch();
}
return $this->current;
}
// Retrieve value of next element
public function next () {
$this->current = $this->fetch();
$this->position++;
return $this->current;
}
// Retrieve key of current element
public function key () {
return $this->position;
}
// Check if current position is valid - used to stop an iterator
public function valid () {
if ($this->position >= 0 && $this->position < $this->rowCount()) {
return true;
}
return false;
}
// Rewind the iterator to the first element
public function rewind () {
$this->checkNoResultException();
$this->position = 0;
$this->_result->data_seek(0);
}
And here is 'query':
public static function query ( $stmt, $params = null ) {
$objInstance = self::getInstance();
try {
if ($params === null) {
return $objInstance->query($stmt);
}
else {
return $objInstance->prepare($stmt)->execute($params);
}
}
catch(MysqliException $e) {
error_log("(DB::query) Query failed: " . $e->getMessage());
die("SQL Query failed -- see log");
}
}