PHP-7: mysqli query #2 returns no results unless query #1 is closed - why?
<p>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 <em>unless</em> I <strong>close()</strong> the previous statement, the next statement ... although it seems to run ... produces no results.</p>

<p>(That is to say, the affected-rows count will return zero.)</p>

<p>I am at a loss to explain this behavior . . . </p>

<p>(Incidentally, the necessary function seemed to be <em>statement-</em> closing.  Not disposing of result sets.)</p>

<p>= = = </p>

<p>I do <em>not</em> believe that this is a duplicate of the question cited, because that answer is clearly using <strong>mysqli_multi_query</strong> and I am not.  I am performing three different, unrelated queries, in three consecutive loops.</p>

<p>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 <em>not</em> using.  But I would be <strong>delighted</strong> to learn that I am wrong here.</p>

<p>Code example</p>

<pre><code>echo "Test: simple query.\n"; 
$iter = DB::query('select count(*) from address_book'); 
echo "Query returned " . $iter-&gt;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-&gt;rowCount() . " results.\n"; 

foreach ($iter as $v) 
    print_r($v); 

 $iter = null;  // DITTO ...
</code></pre>

<p>And here are the iterator-support functions:</p>

<pre><code>// Retrieve current element
public function current () {
    if ($this-&gt;current == null) {
        $this-&gt;current = $this-&gt;fetch();
    }

    return $this-&gt;current;
}

// Retrieve value of next element
public function next () {
    $this-&gt;current = $this-&gt;fetch();
    $this-&gt;position++;

    return $this-&gt;current;
}


// Retrieve key of current element
public function key () {
    return $this-&gt;position;
}


// Check if current position is valid - used to stop an iterator
public function valid () {
    if ($this-&gt;position &gt;= 0 &amp;&amp; $this-&gt;position &lt; $this-&gt;rowCount()) {
        return true;
    }

    return false;
}

// Rewind the iterator to the first element
public function rewind () {
    $this-&gt;checkNoResultException();
    $this-&gt;position = 0;
    $this-&gt;_result-&gt;data_seek(0);
}
</code></pre>

<p>And here is 'query':</p>

<pre><code>public static function query ( $stmt, $params = null ) {
    $objInstance = self::getInstance();

    try {
        if ($params === null) {
            return $objInstance-&gt;query($stmt);
        }
        else {
            return $objInstance-&gt;prepare($stmt)-&gt;execute($params);
        }
    }
    catch(MysqliException $e) {
        error_log("(DB::query) Query failed: " . $e-&gt;getMessage());
        die("SQL Query failed -- see log");
    }
}
</code></pre>