title: Creating array of fields then looping through set in PHP
body: <p>I'd like to simplify how my app builds a query and loops through the results.  Most of the actual code works, except that PHP does not display all of the results fetched from my query.</p>
  
  <p><strong>PREFACE: example code ahead, do not use in production.</strong></p>
  
  <p>First I build the query.</p>
  
  <pre><code>$arr_fields = array('color', 'number', 'size');
  $select_fields = implode(', ', $arr_fields);
  $q = "SELECT $select_fields FROM supplychain";
  </code></pre>
  
  <p>Now I execute the query then loop through the result set.<br>
  <em>Pro tip: don't use this code for anything important - it's for demo purposes only.</em></p>
  
  <pre><code>echo "&lt;table&gt;";
      $res = doQuery($q);
  
      // create report body
      foreach($res as $r)
      {
  
           // set values from array of field names
           $row_fields = '';
           foreach ($arr_fields as $f)
           {
              // set current $arr_field value inside a table cell...
              $row_fields.= "&lt;td&gt;$r[$f]&lt;/td&gt;";
           }
  
           // display the row
           echo "&lt;tr&gt;$row_fields&lt;/tr&gt;";
  
      }
  echo "&lt;/table&gt;";
  </code></pre>
  
  <p>The first database value from <code>$arr_fields</code> (<code>color</code>) is output as expected, the rest are not. </p>
  
  <p>The assembled query issued at a MySQL console shows values for <code>color, number and size</code>.  There aren't any SQL errors generated by the script.</p>
  
  <p>Why is PHP displaying the first field's value but skipping the other two?</p>