zhuanzhou
zhuanzhou

Reputation: 2443

how to print the count number in mysql?

a query :

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

if i assign it to $result .

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

how to output the count number? after the $obj->content_id. there is no number printed. thank you.

Upvotes: 1

Views: 527

Answers (5)

Andrew
Andrew

Reputation: 2568

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

And then count should be in $obj->the_count

Upvotes: 1

moteutsch
moteutsch

Reputation: 3831

Change

COUNT(*) FROM

to

COUNT(*) as count FROM

Upvotes: 1

Andomar
Andomar

Reputation: 238078

Give the count an alias, like:

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

Then you can refer to it by that name:

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

Upvotes: 3

Matthieu Napoli
Matthieu Napoli

Reputation: 49533

You need an alias for your column COUNT(*) (i.e. rename the column) :

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

Then use the variable :

$obj->count

Upvotes: 3

George Cummins
George Cummins

Reputation: 28906

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

Upvotes: 2

Related Questions