Bickey Posted December 22, 2010 Share Posted December 22, 2010 HOw to get the AVERAGE of my entire database? $query = "SELECT ROUND(AVG(scores)) FROM table"; Thanks! Bickey Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/ Share on other sites More sharing options...
nafetski Posted December 22, 2010 Share Posted December 22, 2010 With any of the mysql aggregate functions (SUM, AVG, etc) you'll need to have a GROUP BY clause Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150380 Share on other sites More sharing options...
Bickey Posted December 22, 2010 Author Share Posted December 22, 2010 Hi nafetski. I'm uncertain , what if I want to get the average and there's nothing to be GROUP BY. (I don't have similar values in any of the rows). Thanks. Bickey Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150385 Share on other sites More sharing options...
BlueSkyIS Posted December 22, 2010 Share Posted December 22, 2010 You do not need a group by clause for that SQL. it should work as written. have you tried it? Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150388 Share on other sites More sharing options...
Bickey Posted December 22, 2010 Author Share Posted December 22, 2010 yes, I did try it. The result I got was "Resource id #2" Bickey Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150393 Share on other sites More sharing options...
BlueSkyIS Posted December 22, 2010 Share Posted December 22, 2010 that's a problem with your PHP. can you post your code? Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150395 Share on other sites More sharing options...
Bickey Posted December 22, 2010 Author Share Posted December 22, 2010 The code is infact too long. but is this where I'm doing wrong. TO display the result (used in an email) <br>"The average score is ".$query."<br> Bickey Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150397 Share on other sites More sharing options...
BlueSkyIS Posted December 22, 2010 Share Posted December 22, 2010 can we at least see the part of the code where $query is executed and the value of $query is set?? Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150403 Share on other sites More sharing options...
Bickey Posted December 22, 2010 Author Share Posted December 22, 2010 These are the only two lines which is used to get the AVG and display (in an email). $query = "SELECT ROUND(AVG(scores)) FROM table"; //email <br>"The average score is ".$query."<br> Bickey Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150411 Share on other sites More sharing options...
BlueSkyIS Posted December 22, 2010 Share Posted December 22, 2010 apparently you never execute the SQL in $query. But I can't be sure as there isn't enough code to tell. Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150413 Share on other sites More sharing options...
Rifts Posted December 22, 2010 Share Posted December 22, 2010 $query = "SELECT ROUND(AVG(scores)) FROM table"; change to $query = mysql_query("SELECT ROUND(AVG(scores)) FROM table"); Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150428 Share on other sites More sharing options...
Bickey Posted December 23, 2010 Author Share Posted December 23, 2010 That did not work. Bickey Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150613 Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2010 Share Posted December 23, 2010 Of course it didn't; the query is never executed. $query = "SELECT ROUND(AVG(scores)) AS avg FROM table"; $result = mysql_query($query); $array = mysql_fetch_assoc($result); echo "<br>The average score is {$array['avg']}<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/222399-roundavgscores/#findComment-1150633 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.