Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/8869-getting-and-average-from-a-mysql-table/
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]
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.