I'm new to php and I try to build my own blog system. Until now everything works fine except for one issue.
I try to show the number of comments for each blog post. But it won't work and I can't figure out why.
Here is the code:
include('db URL');
$query = $db->prepare("SELECT post_id, ... cat_name, cat_id FROM posts ORDER BY nse_p_id DESC");
$query->execute();
$query->bind_result($post_id, $cat, $cat_id);
Now to the HTML part
<div class="media-body span8">
<?php
while($query->fetch()):
?>
<div class="blogpostfield">
<div class="blogpostinfo">
Category <?php echo "<a href='sort.php?cat_id=$cat_id'><b>".$cat."</b></a>"?>
with <?php
$comCount = $db->query("SELECT * FROM comment WHERE com_post_id = '$post_id'");
if($comCount){
echo $comCount;
}else{
echo "ERROR - count unsuccessful";
}
?>
comments
</div>
</div>
<?php endwhile?>
</div>
This is just a snipped. Everthing else works fine. The problem is imo that the query within the while loop doesn't work. Can anyone help?
Thanks in advance.
Andy
I guess you want the number of rows in your result.
$result = $db->query("SELECT * FROM comment WHERE com_post_id = '$post_id'");
$comCount = $result->num_rows;
You could also just count the number of rows in the query:
$result = $db->query("SELECT COUNT(*) FROM comment WHERE com_post_id = '$post_id'");
$comCount = $result->fetch_field;
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.