Jump to content

Archived

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

billshackle

Getting and Average from a MySql table

Recommended Posts

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;

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

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.

Share this post


Link to post
Share on other sites
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:
[code]<?php
$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'];
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.