Astro Posted June 5, 2011 Share Posted June 5, 2011 Hey, I have a basic understanding of php but I need a little help to do something. I have rows in database table `users` called 'views', 'favourites' and 'subscribers' etc. The values in each of them rows are expressed as numbers ie 'user1 has 35 subscribers' I want to be able to create a simple feature which reads this data and converts it into one total number. e.g. data for user1 profile views: 150 favourites: 50 subscribers: 10 total: 210 Which i can then use as a basic foundation for a points system. I don't know how to count using sql and pull the data and merge it all. Can someone help or assist me with this problem? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/238490-help-with-sql-count/ Share on other sites More sharing options...
WebStyles Posted June 5, 2011 Share Posted June 5, 2011 I'm assuming you know how to connect to a database and use a simple mysql_query. So try something like this (assuming you have a function called conn() that handles the database connection and that there is a field in the database called username): $username = 'Johnny Bravo'; $conn = conn('database_name'); $q= mysql_query("select `views`,`favourites`,`subscribers` from `users` where `username`= '$username'",$conn); $r = mysql_fetch_assoc($); @mysql_close($conn); if(!empty($r)){ $total = $r['views'] + $r['favourites'] + $r['subscribers']; echo $total; }else{ echo 'username not found in database'; } Quote Link to comment https://forums.phpfreaks.com/topic/238490-help-with-sql-count/#findComment-1225531 Share on other sites More sharing options...
wildteen88 Posted June 5, 2011 Share Posted June 5, 2011 You can add up the field within your SQL query. An Example query SELECT (col1 + col2 + col3) AS total FROM table Quote Link to comment https://forums.phpfreaks.com/topic/238490-help-with-sql-count/#findComment-1225533 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.