View Single Post
Old 02-02-2008, 01:21 PM   #1 (permalink)
kizzeith
 
True Techie

Join Date: Nov 2003

Location: Minneapolis, MN

Posts: 157

kizzeith is on a distinguished road

Send a message via Yahoo to kizzeith
Default Why is this PHPscript not working???

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.
__________________
Gateway Machine - I know...I sold out...if I could do it over I'd have built like I used to
MS Vista Premium 32-bit
AMD Phenom (9500) Quad-core Processor 2.20 ghz
500GB hard drive
3GB DDR2 RAM
2 Free PCI-E slots x1
1 Free PCI-E x16
kizzeith is offline