0

i have this code

<?php 
    $votes_query=mysql_query("select * from votes where CandidateID='$id'");
    $vote_count=mysql_num_rows($votes_query);
    echo $vote_count;
?>

which fetches individual *ID*s from my votes table.

I need a code which can help fetch the sum of specific *ID*s.

Lets say, I have ID-205 and ID-209 in my table.

doptimusprime
  • 9,115
  • 6
  • 52
  • 90

2 Answers2

0
<?php
$ids = array('205','209');

$votes_query=mysql_query("select * from votes where CandidateID IN($ids) ");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>

Put all ID's in an array & Use IN Clause of mysql.

Ankit Pise
  • 1,243
  • 11
  • 30
0
<?php 
    $ids = array('205','209');

    $sql  "SELECT SUM(field_score) AS vote_score FROM votes WHERE CandidateID IN ($ids)";

    $vote_score = mysql_query($sql);
    echo $vote_score;    
?>
Krucamper
  • 371
  • 3
  • 5