Jump to content

Problem with getting average to display from mysql


kpetsche20

Recommended Posts

I'm trying to get the rating of the column rating in the table rating, when I execute that code all that displays is

Resource id #8

 

 

<? 
$getaverage = "SELECT AVG(rating) FROM rating";
$runaverage = mysql_query($getaverage) or die(mysql_error());
echo $runaverage;
while($data = mysql_fetch_assoc($runaverage))
{
echo "".$data['rating']."";
}
?>

mysql_query() returns a Resource ID, so that's what your code prints.

 

Use an alias for the average:

 

$getaverage = "SELECT AVG(rating) as avrating FROM rating";
$runaverage = mysql_query($getaverage) or die(mysql_error());

 

Only one row so no need for the while loop

 

echo mysql_result ($runaverage, 0, 'avrating');

 

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.