I'm currently creating my own blog with a custom css built from scratch with PHP. I am still very new to the language and also with SQL. Right now I have a problem with displaying a Comments(3)
link on each post that I query from the database. I have 3 tables: users, posts, and comments.
USERS
id | username | password | name | email | privileges
POSTS
postid | title | date | content | userid | visible | active
COMMENTS
commentid | c_name | c_email | c_ website | c_date | c_content | approved | postid
This is my current query for displaying posts content from the 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>
I tried doing the below query to retrieve the number of comments by finding it by its post id INSILDE the while loop.
<?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'];
?>
But the results ended up that I only got one post displayed with the right number of comments... How do I get these results in one query?
A single query could work like:
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
Then the inner query is not necessary at all because the primary query returned the comment count as a field.
I believe you have used the same variable name $query
for both fetching post data and comment number. When you are using the same variable name $query $query = connect()->prepare("SELECT COUNT(postid) as commmentno FROM comments WHERE postid = :postid");
it overwrites the previous query result. So that you have only one posted displayed with right comment number. Try change your codes to
<?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'];
?>
You can try this:
$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>
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.