2011-05-20 61 views
1

查詢:如何在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。沒有打印號碼。謝謝。

回答

3

你需要一個別名爲您列COUNT(*)(即重命名列):

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

然後使用變量:

$obj->count 
+0

謝謝。得到它了 – zhuanzhou 2011-05-20 13:24:51

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

它缺少一些引號。 – 2011-05-20 13:24:40

+0

+1,但我會使用一個不能被誤認爲'COUNT()'函數的名稱。 – Piskvor 2011-05-20 13:24:41

3

給這個算一個別名,像:

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

那麼你可以參考它由名稱:

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

變化

COUNT(*) FROM 

COUNT(*) as count FROM 
1

嘗試增加:$查詢=請求mysql_query(選擇的content_id,COUNT(* )* as the_count * FROM votingapi_vote WHERE value_type = option AND value = 1 GROUP BY content_id)

然後計數應該在$ obj-> the_count