我想计算在我的页面上发布的总评论/帖子。我的数据库中有一个名为test的表。在表格中我有一个名为comment的列,其中每个帖子都已存储。我遇到的问题是回应评论总数并不断更新,因为观众不断发布评论,我尝试使用此代码
<?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.请帮助我。
答案 0 :(得分:0)
试试这个:
list($count) = mysql_fetch_row(mysql_query("select count(*) from `test`"));
echo $count;
或者,如果您已经在运行查询以获取一些注释,则可以尝试:
$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