0

i am converting my code to PDO to make my website less vulnerable
this is my code and according to another post on here this should work:

$stmt = $handler->prepare("SELECT * FROM news ORDER BY date DESC LIMIT 5");
$run = $stmt->execute();
if(!$run){
    echo 'sorry';
}
while($row = $query->fetch(PDO::FETCH_ASSOC)){
    echo $row['title'];
  $article_id = $row['article_id'];
  $user_id = $row['user_id'];
  $title = $row['title'];
  $content = $row['content'];
  $date = $row['date'];
  $stmt2 = $handler->prepare("SELECT * FROM users WHERE id = :id");
  $stmt2->bindParam(':id',$id);
  $stmt2->execute();
  $row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
  $user_name = $row2['username'];
  $title2 = str_replace(" ","-",$title);
  echo '<div class="row">
  <div class="col-lg-12"><h3 class="para"><a class="para" href="http://news.red-sec.net/article/'.$article_id.'/'.$title2.'">'.$title.'</a></h3>
  <p class="para">Written by: '.$user_name.'</p>
  </div>

i am trying to echo $row['title']; to see if it works but it doesnt echo anything out so i am guessing that there is a mistake somewhere.

EDIT:
It doesn't echo sorry so i know that the statement is being run...

red security
  • 193
  • 3
  • 14

1 Answers1

2

you are running fetch method on $query variable. Try running it on the $stmt object like so:

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  //your code here
}
flynorc
  • 829
  • 6
  • 13
  • I didnt even notice wow... Thank you man ill fox that when i get on my pc – red security Jan 21 '17 at 00:32
  • 1
    In addition to this answer - as you are using a `select *` you would need to be 100% sure that the `title` column exists and you are spelling it correctly. I know it may seem to be a hassle, but there are a number of reasons you should reference the columns you want to retrieve - see http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select for a good list of these – CCBlackburn Jan 21 '17 at 00:43
  • i actually need all the columns from the table for later use. your solution fixed it, thank you. i just needed a fresh pair of eyes i guess – red security Jan 21 '17 at 00:47