I am trying to view table data from a MYSQL db. I want the script to total the “votes” field and display the total votes. Also, I want the percent field (percent of votes) to be calculated by automatically.
Table structure - Name: Caucus
Fields:
Field_id - primary (auto incr)
candidate (varchar)
votes (int)
percent (int)
<?php
require_once ('mysql.php');
$sql = "select `votes` from `caucus` while 1 ";
$list = mysql_query($sql) or die ( " Query failed : " .
mysql_error());
$total = 0;
while ( $lst = mysql_fetch_array ( $list ) )
{
$total += $lst [ 0 ] ;
}
mysql_free_result($list);
echo ( "Total votes: " . $totval . "<br>");
// again, now to show %:
// I assume field 0 is candidate, field 1 is votes
$sql = "select * from `caucus` while 1 ";
$list = mysql_query($sql) or die ( " Query failed : " .
mysql_error());
while ( $lst = mysql_fetch_array ( $list ) )
{
$perc = $lst [ 1 ] * 100 / $total ;
echo ("Candidate: " . $lst [ 0 ] . ", Percentage: " .
$perc . "<br>" ) ;
}
mysql_free_result($list);
?>
I want the script to display the total number of votes received, then output the percents for each candidate.
I’m stuck on this and it’s driving me nuts.
It says: “Query failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘while 1’ at line 1”
Thanks in advance.