billshackle Posted May 2, 2006 Share Posted May 2, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/8869-getting-and-average-from-a-mysql-table/ Share on other sites More sharing options...
bbaker Posted May 2, 2006 Share Posted May 2, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/8869-getting-and-average-from-a-mysql-table/#findComment-32595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.