简体   繁体   中英

SQL query on how to Count the number of comments each post have

I have two database table called "comments" and "posts"

In the "posts" table I got post_id, post_title

In the "comments" table I got comment_id, post_id, message

The post_id in the comments table stores the id of the post that was being commented. This way I can count how many comments a post have.

I tried doing research and end up this code below:

$displaypost = "SELECT * FROM posts";
$result = $conn->query($displaypost);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        $postid = $row['post_id'];
        $posttitle =$row['post_title'];

        $countdata = "SELECT COUNT(post_id) FROM comments WHERE post_id='$postid'";
        $countresult = $conn->query($countdata);
        $countrow = mysqli_fetch_row($countresult); 
        $total_comment = $countrow[0];
        echo "Post Title: $posttitle";
        echo "Post Comment: $total_comment";
    }
} else {
    echo "0 results";
}

The code above results to:

Unable to fetch mysqli_fetch_row()

You only need one query, replace "SELECT * FROM posts" by

    SELECT post_title,count(posts.post_id) as Total FROM posts JOIN comments WHERE posts.post_id = comments.post_id GROUP BY posts.post_id

Then you will have

      $posttitle = $row['post_title'];
      $total_comment =$row['Total'];

      echo "Post Title: $posttitle";
      echo "Post Comment: $total_comment";

Final code

 $displaypost = "SELECT post_title,count(posts.post_id) as Total FROM posts JOIN comments WHERE posts.post_id = comments.post_id GROUP BY posts.post_id";
 $result = $conn->query($displaypost);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
      $posttitle = $row['post_title'];
      $total_comment =$row['Total'];

      echo "Post Title: $posttitle";
      echo "Post Comment: $total_comment";
}
} else {
echo "0 results";
}

You can use a SQL Join and Gouping clause to do this all at once

SELECT Posts.*,Count(Comments.*) as CommentCount FROM posts Posts LEFT JOIN comments Comments ON (Post.id = Comments.post_id) GROUP BY Post.id

https://dev.mysql.com/doc/refman/5.0/en/join.html

https://dev.mysql.com/doc/refman/5.1/en/group-by-handling.html

EDIT:

     $query = "SELECT Posts.*,Count(Comments.*) as CommentCount FROM posts Posts LEFT JOIN comments Comments ON (Post.id = Comments.post_id) GROUP BY Post.id";  
     $result = $conn->query($query);
     while($row = $result->fetch_assoc()) {
         echo "Post ID: {$row['id']} has {$row['CommentCount']} Comment(s)! <br />";
     }

In your code WHERE post_id='$postid'";
It should be "WHERE post_id=" + $postid +";"; Because your not adding the variable your just creating a long string with the variable name in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM