nextman Posted May 14, 2009 Share Posted May 14, 2009 i have a column in a mysql table, and id like to find the sum of the values inside it. any way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/158166-finding-the-sum-of-a-column/ Share on other sites More sharing options...
nextman Posted May 14, 2009 Author Share Posted May 14, 2009 just to add to that, i have this code: <?php $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $query = ("SELECT SUM(site_views) FROM pm_videos"); $result = mysql_query($query); echo "$result"; ?> but all i get back is "Resource id #2" Quote Link to comment https://forums.phpfreaks.com/topic/158166-finding-the-sum-of-a-column/#findComment-834297 Share on other sites More sharing options...
nextman Posted May 14, 2009 Author Share Posted May 14, 2009 UPDATE: got it working. heres the code i used, its pretty simple - sums up a column and displays it: <?php $sqla= "SELECT SUM(column_name) AS Total FROM table_name"; $result = mysql_query($sqla) or die (mysql_error()); while ($myrow = mysql_fetch_array($result)) { $total=$myrow["Total"]; echo 'The total is '.$total.''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158166-finding-the-sum-of-a-column/#findComment-834413 Share on other sites More sharing options...
corbin Posted May 15, 2009 Share Posted May 15, 2009 Since you're you're using an aggregate function without a group by, you will not need a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/158166-finding-the-sum-of-a-column/#findComment-834475 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.