PHP SQL,如何查询一篇文章(针对博客文章)中的评论数


PHP SQL, How do I query for the count of comments in a post (for blog posts)?

我目前正在创建自己的博客,使用PHP从头开始构建自定义css。我对这门语言和SQL还是很陌生的。现在我有一个问题,显示每个帖子上的Comments(3)链接,我从数据库查询。我有3个表:用户、帖子和评论。

用户
id | username | password | name | email | privileges
文章

postid | title | date | content | userid | visible | active 

的评论
commentid | c_name | c_email | c_ website | c_date | c_content | approved | postid

这是我当前的查询显示的帖子内容从数据库:

 $query = connect()->prepare(
    "SELECT * FROM posts JOIN users on posts.userid = users.id WHERE visible = 1 ORDER BY postid DESC");
$query->execute();
<div id="posts">
            <?php
               while($posts = $query->fetch(PDO::FETCH_ASSOC)) {
                $id = $posts['postid'];
                $title = $posts['title'];
                $date = $posts['date'];
                $content = $posts['content'];
                $name = $posts['name'];
            ?>
            <div id="entry" class="post-<?php echo $id; ?>">            
                <h1><?php echo "<a href='entry.php?id=".$id."'>" .$title. "</a>"; ?></h1>
                <div class="entry_content">
                    <?php echo $content; ?>
                    <p><span class="author"><?php echo $name; ?> on <?php echo $date;?></span></p>
                  //this is where I want to put the "Comments(3)"
                </div>
            </div>
            <?php } ?>
        </div>

我试着做下面的查询,通过在while循环中找到它的post id来检索评论的数量。

 <?php 
       $query = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid");
       $query->execute(array(':postid' => $id)); 
       $commentnos = $query->fetch();
       echo $commentnos['commentno'];
  ?>

但结果是,我只有一个帖子显示了正确数量的评论…我如何在一个查询中获得这些结果?

单个查询可以这样工作:

SELECT
    posts.id, posts.title, posts.date, posts.content, posts.name,
    (SELECT COUNT(postid) FROM comments WHERE postid = posts.id) as commmentno
FROM posts
JOIN users on posts.userid = users.id
WHERE visible = 1
ORDER BY postid DESC

则根本不需要内部查询,因为主查询将注释计数作为字段返回。

我相信您使用了相同的变量名称$query来获取帖子数据和评论数。当您使用相同的变量名称$query $query = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid");它覆盖以前的查询结果。这样你只有一个帖子显示正确的评论号。尝试将代码更改为

<?php 
       $commentquery = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid");
       $commentquery->execute(array(':postid' => $id)); 
       $commentnos = $commentquery->fetch();
       echo $commentnos['commentno'];
  ?>

你可以试试:

$query = connect()->prepare(
"SELECT * FROM posts JOIN users on posts.userid = users.id WHERE visible = 1 ORDER BY postid DESC");
$query->execute();
<div id="posts">
        <?php
           while($posts = $query->fetch(PDO::FETCH_ASSOC)) {
            $id = $posts['postid'];
            $title = $posts['title'];
            $date = $posts['date'];
            $content = $posts['content'];
            $name = $posts['name'];
        ?>
        <div id="entry" class="post-<?php echo $id; ?>">            
            <h1><?php echo "<a href='entry.php?id=".$id."'>" .$title. "</a>"; ?></h1>
            <div class="entry_content">
                <?php echo $content; ?>
                <p><span class="author"><?php echo $name; ?> on <?php echo $date;?></span></p>
              //this is where I want to put the "Comments(3)"
             $cquery = connect()->prepare("SELECT COUNT(commentid) as commentno FROM comments WHERE postid='".$id."'");
             $cquery->execute();
             $result = $cquery->fetch(PDO::FETCH_ASSOC);
             echo $result['commentno'];
            </div>
        </div>
        <?php } ?>
    </div>