wiggst3r Posted January 13, 2009 Share Posted January 13, 2009 Hi I have several rows in a datebase, each with a count field. So rows have the following fields: id, friends_count, date Row 1: 1, 4, 2008-11-19 00:00:00 Row 2: 2, 5, 2008-11-20 00:00:00 Row 3: 3, 1, 2008-11-21 00:00:00 etc What I want to do, is loop through the database and get a total of the friends_count. So the above rows would produce 10. How would I do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/ Share on other sites More sharing options...
revraz Posted January 13, 2009 Share Posted January 13, 2009 Look at the SUM function for MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/#findComment-736266 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 SELECT SUM(friends_count) FROM tablename Should do it. Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/#findComment-736267 Share on other sites More sharing options...
wiggst3r Posted January 13, 2009 Author Share Posted January 13, 2009 I used the following code: //Get total of vouchers from cached table $friends_cached = $db->db_query("SELECT SUM(total_friends) FROM daily_entries_cache"); echo "Total Friends is $friends_cached"; And go the following error: Total Friends is Resource id #12 What am I doing wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/#findComment-736286 Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 You need to mysql_fetch_row on the resource. Then access it by the index in the array [0] Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/#findComment-736289 Share on other sites More sharing options...
wiggst3r Posted January 13, 2009 Author Share Posted January 13, 2009 How would I access it? My code is: $friends_cached = $db->db_fetch($db->db_query("SELECT SUM(total_friends) FROM daily_entries_cache")); Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/#findComment-736311 Share on other sites More sharing options...
trq Posted January 13, 2009 Share Posted January 13, 2009 The $db object you are using is not standard php. I would suggest reading the docs for that object or at least taking a look to see what its db_query() and db_fetch() methods return. Quote Link to comment https://forums.phpfreaks.com/topic/140682-phpmysql-c/#findComment-736316 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.