<?php
$totalvotes = ("SELECT COUNT(*) AS total FROM voting WHERE votes >= 0 ");
$totalvotesresults = mysql_query( $totalvotes )
or die( "Could not get total votes " .mysql_error() );
$data = mysql_fetch_array( $totalvotesresults );
echo "<div>Total number of votes is ". $data['votes'] ."</div>\n";;
?>
Asked
Active
Viewed 34 times
0

John Conde
- 217,595
- 99
- 455
- 496

user3397408
- 7
- 2
-
1What error do you get? – John Conde Mar 10 '14 at 13:42
-
Assuming each row in your database is worth one vote, you could use `$totalVotes = mysql_num_rows( $totalvotesresults );` (http://ca3.php.net/mysql_num_rows) – Tony M Mar 10 '14 at 13:44
1 Answers
4
You call the result of your COUNT(*)
query total
but use votes
in your PHP:
echo "<div>Total number of votes is ". $data['votes'] ."</div>\n";
should be:
echo "<div>Total number of votes is ". $data['total'] ."</div>\n";
Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
- 1
- 1

John Conde
- 217,595
- 99
- 455
- 496
-
I added a slight change to the white-spacing on your answer John, but otherwise you are right on. – PseudoNinja Mar 10 '14 at 13:45