本文介绍了如何计算总评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算在我的页面上发布的评论总数.我的数据库中有一个名为test的表.在表中,我有一列名为comment的列,每个帖子都存储在其中.问题是要回显评论的总数并随着观众不断在此处发表评论而不断更新,而我尝试使用此代码

I want to count the total comment/post posted on my page. i have a table in my database named test. within table i have a column named comment, where every post is been stored. the problem am having is to echo out the total number of comment and keep updating as viewers keep on posting there comment and i tried using this code

<?php
 $handle = mysql_query("SELECT `comment`, COUNT(*) AS `count` 
 FROM test GROUP BY `comment` ");

if ($handle) {
$results = mysql_fetch_assoc($handle);
echo ($results[0]['count'] + $results[1]['count']);
}
?>

但是它一直在回显0.请帮帮我.

but it keep on echoing out 0. pls help me out.

推荐答案

尝试一下:

list($count) = mysql_fetch_row(mysql_query("select count(*) from `test`"));
echo $count;

或者,如果您已经在运行查询以获取一些注释,则可以尝试以下操作:

Alternatively, if you are already running a query to get some comments, you can try this:

$sql = mysql_query("select sql_calc_found_rows * from `test` order by `id` desc limit 10");
// ^ Get the 10 most recent comments
list($count) = mysql_fetch_row(mysql_query("select found_rows()"));
// this avoids having to run the entire query again, great for efficiency!
while($comment = mysql_fetch_assoc($sql)) var_dump($comment); // just an example

这篇关于如何计算总评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 10:37