[英]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.