Jump to content

Help with sql count


Astro

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/238490-help-with-sql-count/
Share on other sites

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';
}

Link to comment
https://forums.phpfreaks.com/topic/238490-help-with-sql-count/#findComment-1225531
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.