Why is this PHPscript not working???

Status
Not open for further replies.

kizzeith

In Runtime
Messages
159
Location
Minneapolis, MN
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>

<?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);

?>
</php>
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.
 
To post code contain it within "
Code:
" tags.  Send a PM to user CrazeD, he may be able to help :)
 
'while' is not a MySQL parameter (to the best of my knowledge). I think what you want is WHERE. What are you trying to do with the query?
 
I want it to count up the total in the votes field for all records and echo that...also, I want it to calculate the percentage of total votes for each candidate...
 
Status
Not open for further replies.
Back
Top Bottom