PHP SQL,如何查询帖子中的评论数(对于博客文章)?

时间:2014-06-08 02:54:25

标签: php sql

我目前正在创建自己的博客,其中包含使用PHP从头开始构建的自定义css。我仍然是语言和SQL的新手。现在我在从数据库查询的每个帖子上显示Comments(3)链接时遇到问题。我有3个表:用户,帖子和评论。

USERS

id | username | password | name | email | privileges

帖子

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

COMMENTS

commentid | c_name | c_email | c_ website | c_date | c_content | approved | postid

这是我目前用于显示db:

中的帖子内容的查询
 $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>

我尝试通过下面的查询来检索注释的数量,方法是通过post id INSILDE while while循环来查找它。

 <?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'];
  ?>

但结果最终我只有一篇文章显示了正确的评论数量......如何在一个查询中获得这些结果?

3 个答案:

答案 0 :(得分:2)

单个查询可以像:

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

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

答案 1 :(得分:1)

我相信你使用相同的变量名$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'];
  ?>

答案 2 :(得分:0)

你可以试试这个:

$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>