% from a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

% from a database

Post by m3mn0n »

Say i save 3500 survey results in a database, they include the choices of the survey and the things the visitors picked. Counting the number of total entries in the database is easy, but how would extract a percentage from the survey results? Maybe it's because i had a long nite, but i just can't figure this one out or find a tutorial on this topic.

thanks~
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$query = 'SELECT count(*) from tablename';
$result = mysql_query($query, $conn);
$totalnum = mysql_fetch_row($result);
$totalnum = $totalnumї0];

$query = 'SELECT rating,count(*) from tablename group by rating';
$result = mysql_query($query, $conn);
while( $row = mysql_fetch_row($result) )
   echo $rowї0], ' voted ', $rowї1], ' times (', $rowї1]*100/$totalnum, '%)';
Post Reply