title: Populating HTML from MySQL & PDO (PHP); foreach loop failing to write MySQL data
body: <p>As title states, my objective is to produce an HTML table using data from a MySQL database.</p>
  
  <p>The general method I am using is sound, as I have written manually a series of cells, but I am trying to scale it to include large numbers of fields, and as such am trying to write the HTML for the cells using PHP and foreach loops with arrays. I've written out the context and steps of my working to both give context to my problem and provide general help to people in a similar situation. I know, however, that the problem I have lies specifically in one section of code - indicated clearly below if you want to skip right to it.</p>
  
  <p><strong>Step 1 define arrays for the titles I'd like the fields to have and the column names in the MySQL db:</strong></p>
  
  <pre><code>$titles  = array('Name', 'Age', 'Height', 'Weight');
  $headers = array('q1', 'q2', 'q3', 'q4');
  </code></pre>
  
  <p><strong>Step 2: connect to the database ($username and $password previously defined):</strong></p>
  
  <pre><code>try {
      $pdo = new PDO('mysql:host=localhost; dbname=db01', $username, $password);
      $pdo-&gt;exec("SET CHARACTER SET utf8");
  </code></pre>
  
  <p><strong>Step 3: Create my SQL statement via a few concatenations, and fetch the $result:</strong></p>
  
  <pre><code>$sql ="SELECT";
  
  foreach(array_combine($headers, $titles) as $header =&gt; $title) {
      $sql .= "`$header` AS `$title`,";
  }
  
  $sql .= "`q5` AS `Eye Color`";
  $sql .= "FROM samdata.CO_data";
  
  
  $result = $pdo-&gt;query($sql);
  </code></pre>
  
  <p><strong>Step 4: Create $html_table by concatenating static HTML and the results from the above SQL query</strong></p>
  
  <pre><code>if($result !== false) {
      $html_table = '&lt;table&gt;&lt;tr&gt;';
      foreach($titles as $title) {
          $html_table .= "&lt;th&gt; $title &lt;/th&gt;";                          
      }
  
      $html_table .='&lt;/tr&gt; &lt;tr&gt;';
  
  
  //*********PROBLEM SECTION BELOW **************************
      foreach(array_combine($result, $titles) as $row =&gt; $title) {                            
          $html_table .= "&lt;td&gt;' .$row\['$title'\]. '&lt;/td&gt;";
      }
  }
  //*********PROBLEM SECTION ABOVE **************************
  
  $html_table .= '&lt;/tr&gt; &lt;/table&gt;';
  $conn = null;   
  
  echo $html_table;       
  }
  </code></pre>
  
  <p>I know the problem lies with the section indicated, because I've test all the other parts, and if instead of combining arrays / foreach loops I manually write out, for example:</p>
  
  <pre><code>foreach($result as $row) {
               $html_table .= '
               &lt;tr&gt;
                  &lt;td&gt;' .$row['Names']. '&lt;/td&gt;
                  &lt;td&gt;' .$row['Age']. '&lt;/td&gt;
                  &lt;td&gt;' .$row['Height']. '&lt;/td&gt;
              &lt;/tr&gt;';
  }
  </code></pre>
  
  <p>In place of the loop, then it works fine and displays the data for each result for those headings, the problem being I have hundreds and want to be more elegant (and lazy) than writing them all out one by one!</p>
  
  <p>Where am I going wrong? Many thanks in advance for your help.</p>