[英]PHP SQL, How do I query for the count of comments in a post (for blog posts)?
我目前正在使用自定义CSS创建自己的博客,该CSS是使用PHP从头开始构建的。 我对语言和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>
我尝试执行以下查询,以通过其帖子ID INSILDE 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'];
?>
但是结果结果是,我只显示了一条带有正确数量的评论的帖子...如何在一个查询中获得这些结果?
单个查询的工作方式如下:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.