title: How do you fetch all rows with pdo?
body: <p>I am making a blog and I want to fetch all rows using a pdo statement but no matter what I do only one row comes back even though there are two rows in my database.</p>
  
  <p>Here's the code sample where I connect:</p>
  
  <pre><code>&lt;?php
  try{
  $link=new PDO('mysql:host=127.0.0.1;dbname=blog1','root','');
  $link-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  
  
  
   } catch(PDOException $e){
   echo $e-&gt;getMessage();
     die();
    }
  ?&gt;
  </code></pre>
  
  <p>Then I try to fetch all rows</p>
  
  <pre><code>&lt;?php
     require 'Escape.php';
    $posts_query=$link-&gt;query('SELECT * FROM posts');
    $posts_query-&gt;execute();
  /* variable that holds a with the database link and query in it then fetches 
    all the data related to the query into and assoc array */
  $result=$posts_query-&gt;fetchAll();
  
   //counting all rows
   $count=$posts_query-&gt;rowCount();
    if($count&gt;0){
  
  
  
   foreach($result as $r){
        $id= $r['id'];
      $title= $r['title'] ;
     $content=$r['content'];
         $date= $r['date'];
  
  //admin buttons
   $admin="";
  //keeping title safe
      $Title=htmlentities($title);
      //keeping output safe
      $output=htmlentities($content);
   // styling the posts to be echoed with secure variables
  $posts= "&lt;div&gt;&lt;h2&gt;&lt;a href='view_post.php?pid=$id' class='names'&gt;$Title&lt;/a&gt; 
    &lt;/h2&gt;&lt;h3&gt;$date&lt;/h3&gt;&lt;p&gt;$output&lt;/p&gt;$admin&lt;/div&gt;";
    escape($posts);
     }
   echo"&lt;div id=posts&gt;$posts&lt;/div&gt;";
    } else{
  echo 'There are no posts to display.';
    }
  
      ?&gt;
  </code></pre>