0

Why does my SELECT query not work? I know there are multiple ways to cut down the pdo query code but I wanted to use the longer version in hope that I would find the problem. The $CommentID when echoed is displaying the right ID but the variables do not get populated with the corresponding data.

<?php
    $CommentID = $_GET['id'];

    $ViewQuery = $pdo->prepare('SELECT * FROM comments WHERE id=:id');
    $ViewQuery->bindParam(":id",$CommentID);
    $ViewQuery->execute();
    $comment = $ViewQuery->fetch();
    $DateTime=$comment['datetime'];
    $ComName=$comment['name'];
    $Email=$comment['email'];
    $Comment=$comment['comment'];
    $PostID=$comment['admin_panel_id'];
?>

I have looked at multiple similar questions but they all have syntax error and I don't think that's the case here, maybe it is, I just don't see it.

user1506104
  • 6,554
  • 4
  • 71
  • 89
Madda
  • 21
  • 5
  • Do a `var_dump($comment);` to see what is returned by the fetch. – Gordon Oct 18 '17 at 05:25
  • @Gordon returns bool(false) – Madda Oct 18 '17 at 05:27
  • So there was an error fetching the row. Do `var_dump($ViewQuery->errorInfo())` now to find out what error. – Gordon Oct 18 '17 at 05:29
  • @Gordon returns array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } I think I know what's going on. The wrong ID is getting passed. – Madda Oct 18 '17 at 05:31
  • where did you put that var_dump? please put if right after the fetch.If you want to know what gets passed in the query you can use `$ViewQuery-debugDumpParams();` after the `->execute()` – Gordon Oct 18 '17 at 05:32
  • @Gordon I did put var_dump after the fetch. The problem was that I used the post ID and not the comment ID. – Madda Oct 18 '17 at 05:35
  • oh well. nevermind then. but keep these steps in mind when you dont get the expected result next time. it's basic debugging. – Gordon Oct 18 '17 at 05:36
  • @Gordon Thank you, I will study more on debugging. – Madda Oct 18 '17 at 05:37

0 Answers0