Unable to get more than one row returned using PDO
<p>I am trying to perform a simple query to a MS SQL server database using (client) PHP PDO inside a loop and I am not able to retrieve more than one record.  </p>

<p>The outer loop has a couple hundred thousand iterations (not using PDO) and the inner PDO fetch loop should return 2-4 records for each outer iteration.</p>

<p>This is what I have,  not sure what I am doing wrong:</p>

<pre><code>$dbh = '';
try {      
  $dbh = new PDO("sqlsrv:Server=mysqlserver, 1433;Database=mydatabase", '', '');
}
catch(Exception $e) {
     echo "Error connecting to MS SQL database.";
     error_log($e);
     exit;
}

$foreign_keys = array(1563,89563,98272634); // etc..  The actual keys used here are obtained with other code not related to the problem.
foreach($foreign_keys as $foreign_key){        
    $sth = $dbh-&gt;prepare("select col1 from one_to_many_table where foreign_key=?");
    $sth-&gt;bindParam(1, $foreign_key );
    $sth-&gt;execute();
    // expecting 2+ rows here ..
    while($result = $sth-&gt;fetch( PDO::FETCH_ASSOC )){                
            echo $foreign_key.', '.$result['col1']."\n";                
    }
}

// Output:
// 1563, {value for col1}  // first outer iteration, first PDO record returned
// --END -- Nothing else
</code></pre>

<p>I get just the first record returned from the PDO fetch from the first iteration of the outer loop.</p>

<p>I don't get any errors displayed on the screen or in the error log.</p>

<p>I am running this script from the CMD line on a Windows 7 machine with PHP 5.6.26 (cli).   </p>