Search code examples
mysqlpdoforeachfetchall

PDO foreach printing out a single entry from wrong column


Sorry for the awkward title, I have a query that is supposed to return all the comments that have the value 1 in the blog column.

The table is made up of three columns:

CommentID Comments Blog

At the moment the query is returning all the comments with blog value of 1 however it is also printing out the CommentID of the most recently entered comment along with the value 1 (the value 1 is still printed even if the WHERE value is changed from 1 to 2).

Here is my query:

  public function getComments($blog) {
    $conn = $this->dbConnection->Connection();
    $sth = $conn->prepare("SELECT `Comment` FROM comments WHERE Blog = ?");
    $sth->bindValue (1, $blog);
    $sth->execute();
    $result = $sth->fetchAll();
    foreach ($result as $row) {
        print $row['Comment'] . "\n";
    }
    $conn = $this->dbConnection->closeConnection();
  }

I am looking it just to print out the contents of Comment WHERE Blog = 1, what is causing my problem?


Solution

  • There is some other code in your PHP file that does print extra numbers. Check it out. The code you posted here intended to print the Comment field only (though it may contain some extra numbers as well)