1

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.

Here's the code sample where I connect:

<?php
try{
$link=new PDO('mysql:host=127.0.0.1;dbname=blog1','root','');
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);




 } catch(PDOException $e){
 echo $e->getMessage();
   die();
  }
?>

Then I try to fetch all rows

<?php
   require 'Escape.php';
  $posts_query=$link->query('SELECT * FROM posts');
  $posts_query->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->fetchAll();

 //counting all rows
 $count=$posts_query->rowCount();
  if($count>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= "<div><h2><a href='view_post.php?pid=$id' class='names'>$Title</a> 
  </h2><h3>$date</h3><p>$output</p>$admin</div>";
  escape($posts);
   }
 echo"<div id=posts>$posts</div>";
  } else{
echo 'There are no posts to display.';
  }

    ?>
Nick
  • 138,499
  • 22
  • 57
  • 95
Babydoll
  • 29
  • 3
  • 1
    Every time you loop, you reassign `$posts` to the latest post, not adding it. move `echo"
    $posts
    ";` inside the loop.
    –  Mar 14 '19 at 03:40

2 Answers2

1

Your $posts's value is reset to the latest row when you loop, either you append the value of each post using concat . operator:

if($count>0){
 $posts = "";
 foreach($result as $r){
    // Define your variable
    $posts .= "<div><h2><a href='view_post.php?pid=$id' class='names'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
    escape($posts);
   }
  echo"<div id=posts>$posts</div>";
} else { ... }

Or printing each post while looping:

if($count>0){
 $posts = "";
 echo "<div id='posts'>";
 foreach($result as $r){
    // Define your variable
    $posts = "<div><h2><a href='view_post.php?pid=$id' class='names'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
    escape($posts);
    echo $posts;
   }
  echo"</div>";
} else { ... }
0

Looks like you are re-assigning posts on every iteration, try adding the values to an array and imploding them when you would like to echo them out.

  require 'Escape.php';
  $posts_query=$link->query('SELECT * FROM posts');
  $posts_query->execute();

  $result=$posts_query->fetchAll();

  $count=$posts_query->rowCount();
  if($count>0){
    $posts=[];
    foreach($result as $r){
      $id= $r['id'];
      $title= $r['title'] ;
      $content=$r['content'];
      $date= $r['date'];
      $admin="";
      $Title=htmlentities($title);
      $output=htmlentities($content);
      // Add the HTML to the $posts array, also taking advantage of NOWDOCS
      $posts[]= <<< HTML
<div>
  <h2>
    <a href='view_post.php?pid={$id}' class='names'>{$Title}</a> 
  </h2>
  <h3>{$date}</h3>
  <p>{$output}</p>
  {$admin}
</div>
HTML;
      escape($posts);
   }
   echo"<div id='posts'>" . implode('', $posts) . "</div>";
 } else {
   echo 'There are no posts to display.';
}

Here is a reference to PHP HEREDOCS.

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33