简体   繁体   中英

PHP get largest number from mysql table

i have counted the number of voters that voted for certain candidate i want to display which one got the highest voting number. i tried to store them in variables so i can use max() method but i got the error "undefined ".any help please

  <?php 
 $query="select count(*) as total from voting Where ca_id=1";
  //ca_id is the candidate id that voter choose
 $result = mysql_query($query);
 $row = mysql_fetch_assoc($result);
 echo"$row[total]<br>";


 $query="select count(*) as total2 from voting Where ca_id=2";
 $result = mysql_query($query);
 $row2 = mysql_fetch_assoc($result);
 echo"$row2[total2]<br>";

 $query="select count(*) as total3 from voting Where ca_id=3";
 $result = mysql_query($query);
 $row3 = mysql_fetch_assoc($result);
 echo"$row3[total3]<br>";

 $query="select count(*) as total4 from voting Where ca_id=5";
 $result = mysql_query($query);
 $row4 = mysql_fetch_assoc($result);
 echo"$row4[total4]<br>";



?>
SELECT count(1) co, ca_id FROM voting GROUP BY ca_id ORDER BY co DESC LIMIT 5

You don't need to perform four queries for this. You could just use a single query. In your case, you could do:

select ca_id, count(*) as counter from voting group by ca_id order by counter desc

And you can get your result with a single query

As mentioned, PDO is a better alternative in this case for your database-related calls

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box ? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

You could use something like which uses the MySQLi extension.

<?php

$mysqli = new mysqli('host', 'user', 'pass', 'db');

$sql   = "SELECT COUNT(votes) as vote_count, ca_id as candidate_id FROM voting GROUP BY ca_id ORDER BY vote_count DESC";
$result= $mysqli -> query($sql);

// Error Checking
if($mysqli -> error){
   echo 'Error: '.$mysqli -> error;
   exit;
}

while($row = $result -> fetch_object()){

   // Print out each candidate ID and the amount of votes they had.
   echo 'Candidate ID: '.$row -> candidate_id.', Votes: '.$row -> vote_count.'<br/>';

   // If you want to just show the highest voted candidate - put the data in an array
   $votes[$row -> vote_count] = $row -> candidate_id;

}

echo max(array_keys($votes));

This will also cut the amount of queries your running down to just 1.

first of all use group by to retrieve all data in one query, instead of querying for each of the candidates:

SELECT ca_id, count(*) as total FROM voting GROUP BY ca_id

if you need a top voted id use:

SELECT ca_id, count(*) as total FROM voting GROUP BY ca_id SORT BY total 
DESC LIMIT 1

Note: not the most efficient solution but will be way faster then querying for each ca_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM