Jump to content

[SOLVED] getting average from query


contra10

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.