title: Display all rows in column with foreach
body: <p>I am trying to use foreach to echo every row in a column. But am getting:</p>
  
  <blockquote>
    <p>Warning: Invalid argument supplied for foreach()</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  $user = user;
  $pass = pass;
  $dbh = new PDO('mysql:host=localhost;dbname=onlyaskf_registered', $user, $pass);
  
  $stmt = $dbh-&gt;prepare("SELECT COUNT(*), sum(price),part_number,location,price FROM products WHERE code = :usercode");
  $stmt-&gt;bindParam(':usercode', $_SESSION['MM_Username']);
  
  ?&gt;
  
  &lt;?php 
  if ($stmt-&gt;execute()) {
      foreach($stmt-&gt;fetchColumn(2) as $row) {
          echo $row;
      }
  }  
  ?&gt;
  </code></pre>
  
  <p>With this code I can get the first row within the column:</p>
  
  <pre><code>&lt;?php 
  if ($stmt-&gt;execute()) {
      $row = $stmt-&gt;fetchColumn(2);
      echo $row;
  }  
  ?&gt;
  </code></pre>
  
  <p>Column 2 is my part_number column, if I format my php like above it gives me the first instance of that column's row. How do I get it to show each row in the column with foreach? Is that even possible or is there a better method to echo an entire column's rows?</p>
  
  <p>UPDATE:</p>
  
  <p>I changed my code to this but it is only giving me the first row's column value instead of every row:</p>
  
  <pre><code>&lt;?php if ($stmt-&gt;execute()) {
  while($val = $stmt-&gt;fetch()) { 
  echo $val['part_number'];
  }
  }  ?&gt;
  </code></pre>