Jump to content


Getting and Average from a MySql table

  • Please log in to reply
1 reply to this topic

#1 billshackle

  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 May 2006 - 06:40 AM

How would i go about displaying an average? I have a page with data pulled from my main table in my database but i need an average which is in a different table i have tried;

<? $query="SELECT avg( q6 ) FROM `survey`
avg(q6)";?><? print $avg ?>

but it doesn't seem to do anything. 'q6' is the column i need the info from and 'survey' is the table name.

Thanks in advance if anyone can shed some light on this for me.

#2 bbaker

  • Members
  • PipPipPip
  • Advanced Member
  • 127 posts
  • LocationNY

Posted 02 May 2006 - 12:39 PM

it doesn't do anything because you never executed the query. You constructed the query string:
$query="SELECT avg( q6 ) FROM `survey` avg(q6)" (which is incorrect syntax), but didn't execute with mysql_query(). You also didn't fetch the results.

try this:
$query = "SELECT AVG(q6) AS avg FROM survey";
$result = mysql_query($query) or die(mysql_error()."<br />Couldn't execute query: $query");
$row = mysql_fetch_array($result);
echo $row['avg'];

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users