title: Getting invalid argument for foreach error when using an array
body: <p>I can not understand why this error is occurring. From what I can see I the function I use correctly returns an array of data that exists on the database. I use a <code>foreach()</code> to echo out each of the data in the array but it gives me the error: <code>Warning: Invalid argument supplied for foreach()</code>.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>// Retrieve posts
      function retrieve_posts(){
          // start new instance of the database connection 
          global $dbh;
  
          // Get all the posts
          $stmt = $dbh-&gt;prepare("SELECT * FROM jacks_barbers_reviews ORDER BY date DESC");
          $stmt-&gt;execute();
  
          while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {
  
                  $result[] = $row;
          }
  
          return $result;
      }
  </code></pre>
  
  <p>And the (simplified) foreach loop:</p>
  
  <pre><code>&lt;?php
  
          $posts = retrieve_posts();
  
  
              foreach($posts AS $index){
  
                  echo '&lt;div class="test-wrap"&gt;'; //contains the individual testimonials
                  echo '&lt;p&gt;' . $index['post'] . '&lt;/p&gt;';
                  echo '&lt;p style="float:right;font-style:normal;font-size:15px;"&gt;By ' .$index['name']. '  ' .$index['date']. '&lt;/p&gt;';
                  echo '&lt;div style="clear:both"&gt;&lt;/div&gt;';
                  echo '&lt;/div&gt;'; // closes test-wrap
              }
  
  
      ?&gt;
  </code></pre>
  
  <p>So what is causing this error?</p>
  
  <p>Thanks</p>