contra10 Posted January 29, 2009 Share Posted January 29, 2009 i'm using this to get the average of a rating but it only calculates the lates input divided by the number of rows... <?php $query="SELECT * FROM `ratings` WHERE `ratedid`= '$id'"; $nt=mysql_query($query); echo mysql_error(); $countofrate= mysql_num_rows($nt); while($row=mysql_fetch_array($nt)){ $rateresults = "{$row['rate']}"; } $average = $rateresults / $countofrate; print("Rated: $average"); ?> i don't know if COUNT is supposed to be used or if there is a way to add up all the rows... Link to comment https://forums.phpfreaks.com/topic/142991-solved-getting-average-from-query/ Share on other sites More sharing options...
flyhoney Posted January 29, 2009 Share Posted January 29, 2009 SELECT AVG(rate) FROM ratings Link to comment https://forums.phpfreaks.com/topic/142991-solved-getting-average-from-query/#findComment-749787 Share on other sites More sharing options...
milesap Posted January 29, 2009 Share Posted January 29, 2009 <?php $query="SELECT * FROM `ratings` WHERE `ratedid`= '$id'"; $nt=mysql_query($query); echo mysql_error(); $countofrate= mysql_num_rows($nt); $rateresults = 0; while($row=mysql_fetch_array($nt)){ $rateresults = $rateresults + $row['rate']; } $average = $rateresults / $countofrate; print("Rated: $average"); ?> Link to comment https://forums.phpfreaks.com/topic/142991-solved-getting-average-from-query/#findComment-749789 Share on other sites More sharing options...
printf Posted January 29, 2009 Share Posted January 29, 2009 Something like... $query = "SELECT (SUM(rate)/COUNT(*)) AS average FROM ratings WHERE ratedid = '" . $id . "';"; Link to comment https://forums.phpfreaks.com/topic/142991-solved-getting-average-from-query/#findComment-749793 Share on other sites More sharing options...
contra10 Posted January 29, 2009 Author Share Posted January 29, 2009 thanks Link to comment https://forums.phpfreaks.com/topic/142991-solved-getting-average-from-query/#findComment-749802 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.