如何在mysql中打印计数?

时间:2011-05-20 13:19:09

标签: php mysql

查询:

$query=mysql_query(SELECT content_id, COUNT(*) FROM votingapi_vote WHERE value_type = option  AND value = 1 GROUP BY content_id)

如果我将其分配给$result

while ($obj = db_fetch_object ($result)) {
  $output .= $obj->content_id.'<br>' . $obj->count;
}

如何输出计数?在$obj->content_id之后。没有打印数字。谢谢。

5 个答案:

答案 0 :(得分:3)

您的列COUNT(*)需要别名(即重命名该列):

SELECT content_id, COUNT(*) as count FROM [...]

然后使用变量:

$obj->count

答案 1 :(得分:3)

为计数添加别名,例如:

SELECT content_id, COUNT(*) as the_count FROM ...

然后你可以用这个名字来引用它:

$output .= $obj->content_id.'<br>' . $obj->the_count;    

答案 2 :(得分:2)

$query=mysql_query(SELECT content_id, COUNT(*) as count FROM votingapi_vote WHERE value_type = option  AND value = 1 GROUP BY content_id)

答案 3 :(得分:1)

更改

COUNT(*) FROM

COUNT(*) as count FROM

答案 4 :(得分:1)

尝试添加:$ query = mysql_query(SELECT content_id,COUNT(*) * as the_count * FROM votingapi_vote WHERE value_type = option AND value = 1 GROUP BY content_id)

然后计数应该是$ obj-&gt; the_count