djfox Posted January 15, 2009 Share Posted January 15, 2009 What I want to do is have the code add up all the entries in the database and find the average from a field. Ie, the field name "views" in the table "submissions" and the entries in that field in that table are 234, 365, 264, 646, 354. Add up all of those and get the average number, which in this case is 373 (with the number rounded up to a whole number). I can do the rounding of the number up but I`m not sure how to tell the code to add up all of those numbers from a certain field in the database table. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/ Share on other sites More sharing options...
ngreenwood6 Posted January 15, 2009 Share Posted January 15, 2009 I would look into the sum function of mysql. A basic example is: SELECT SUM(views) FROM table a good tutorial is here http://www.tizag.com/mysqlTutorial/mysqlsum.php Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737414 Share on other sites More sharing options...
djfox Posted January 15, 2009 Author Share Posted January 15, 2009 Thanks very much. I`ll give this a go. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737420 Share on other sites More sharing options...
djfox Posted January 15, 2009 Author Share Posted January 15, 2009 Ok, I gave it go according to what http://www.tizag.com/mysqlTutorial/mysqlavg.php showed. So I entered this: <?php $query = "SELECT gallNum, AVG(views) FROM image WHERE gallNum='$g' GROUP BY gallNum"; $result = mysql_query($query) or die(mysql_error()); echo "The average views is ".$row['AVG(views)']; ?> I get a blank as a result. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737428 Share on other sites More sharing options...
ngreenwood6 Posted January 15, 2009 Share Posted January 15, 2009 Sorry about that I do not know why I told you to do the sum function when you are trying to get an average I misread the post. Try this: <?php $query = "SELECT gallNum, AVG(views) AS avg_views FROM image WHERE gallNum='$g' GROUP BY gallNum"; $result = mysql_query($query) or die(mysql_error()); echo "The average views is ".$row['avg_views']; ?> Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737443 Share on other sites More sharing options...
Philip Posted January 15, 2009 Share Posted January 15, 2009 You still need to call mysql_fetch_assoc (or array) Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737445 Share on other sites More sharing options...
ngreenwood6 Posted January 15, 2009 Share Posted January 15, 2009 Wow, good call kingphilip. I am seriously going to bed in like 5 mins because I am missing some obvious stuff. Good thing you are following my posts lol. I do like doing it with the AS section though because it is easier to read. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737447 Share on other sites More sharing options...
Philip Posted January 15, 2009 Share Posted January 15, 2009 Agreed, I would use the AS, it makes things a lot easier Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737450 Share on other sites More sharing options...
djfox Posted January 15, 2009 Author Share Posted January 15, 2009 Ok, so I now have: <?php $query = "SELECT AVG(views) AS avg_views FROM image WHERE gallNum='$g'"; $result = mysql_query($query) or die(mysql_error()); mysql_fetch_assoc($result); echo "The average views is ".$result['avg_views']; ?> But the result is still blank. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737456 Share on other sites More sharing options...
ngreenwood6 Posted January 15, 2009 Share Posted January 15, 2009 Try this: <?php $query = "SELECT AVG(views) AS avg_views FROM image WHERE gallNum='$g'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "The average views is ".$row['avg_views']; } ?> You should look more into mysql's data input and retrieval. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737458 Share on other sites More sharing options...
djfox Posted January 15, 2009 Author Share Posted January 15, 2009 Eureka! We struck it! Thanks guys. Link to comment https://forums.phpfreaks.com/topic/140886-solved-finding-the-average-from-adding-multiple-entries-from-db/#findComment-737462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.