Here's what I'm currently using:
$query = mysql_query(" SELECT vote_count FROM votes ");
while ($row = mysql_fetch_array($query)) {
$total_votes += $row['vote_count'];
}
echo $total_votes;
Is there a more concise way, perhaps in the query itself without h开发者_如何学Goaving to use the while loop?
You can use the MySQL sum function and get the sum from MySQL itself:
SELECT sum(vote_count) AS vote_count_sum FROM votes
Fetch the single row that the query produces in $row
and $row['vote_count_sum']
will have the total.
SELECT SUM( vote_count ) AS vote_summary FROM votes
精彩评论