Jump to content

[SOLVED] Averages from MySQL


Chrisman

Recommended Posts

I'm very new with PHP and MySQL and I need help with getting the averages from my MySQL database.  Basically I'm wanting to make a rating system, and I hope I can just call for an average from the particular table and it will average up all the numbers and display that.  I know there is a avg function but I just couldn't get it to work.

 

Any help will be much appreciated.

Link to comment
Share on other sites

I tried that, and it didn't work.  It just puts out a blank.

 

<?php
// connecting code...

$result = mysql_query("SELECT AVG(rating) AS AVERAGE FROM 5differences")
or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo "<br />Average ratings of 5differences: ".$row['rating'];
// A few other text echo's
?>

 

The page is here  to see the exact output.

Link to comment
Share on other sites

<?php
// connecting code...

$result = mysql_query("SELECT AVG(rating) AS AVERAGE FROM 5differences")
or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo "<br />Average ratings of 5differences: ".$row['rating'];
// A few other text echo's
?>

 

should be

 

<?php
// connecting code...

$result = mysql_query("SELECT AVG(rating) AS AVERAGE FROM 5differences")
or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo "<br />Average ratings of 5differences: ".$row['average'];
// A few other text echo's
?>

 

The name of the value will be whatever is after the "AS". In this case, it is "AVERAGE". If you needed it to be rating, you could easily change it to:

SELECT AVG(rating) AS RATING FROM 5differences

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.