thecard Posted July 11, 2008 Share Posted July 11, 2008 I have a simple SELECT query: $query3 = mysql_query("SELECT hits FROM tb_sites WHERE user='$user'") or die(mysql_error()); $row3 = mysql_fetch_array($query3); Then I echo the output. "Hits" is a number btw. But if there is more than one output (more than 1 occurence of a record with user='$user') I want to add the outputs together and store that number in a variable. How could I do this? Quote Link to comment Share on other sites More sharing options...
vikramjeet.singla Posted July 11, 2008 Share Posted July 11, 2008 $query3 = mysql_query("SELECT hits FROM tb_sites WHERE user='$user'") or die(mysql_error()); $row3 = mysql_fetch_array($query3); instead of use this $query3 = mysql_query("SELECT COUNT(hits) as TotalHits FROM tb_sites WHERE user='$user'") or die(mysql_error()); $row3 = mysql_fetch_array($query3); Quote Link to comment Share on other sites More sharing options...
thecard Posted July 11, 2008 Author Share Posted July 11, 2008 Don't really understand that -> is it meant to add the hits from all the records and store the data in $row3['TotalHits']; ? Because that doesn't work. Quote Link to comment Share on other sites More sharing options...
fenway Posted July 11, 2008 Share Posted July 11, 2008 I think you mean SUM()... Quote Link to comment Share on other sites More sharing options...
vikramjeet.singla Posted July 11, 2008 Share Posted July 11, 2008 as per your query this seem that you are inserting users hits individually.... that why i have mentioned a query with COUNT() not with SUM() as that does not make any sense here.... Quote Link to comment Share on other sites More sharing options...
fenway Posted July 11, 2008 Share Posted July 11, 2008 Actually, they would be equivalent, no? Quote Link to comment Share on other sites More sharing options...
thecard Posted July 11, 2008 Author Share Posted July 11, 2008 Sorry if I wasn't clear to start with. Thanks all. This works perfectly: $query3 = mysql_query("SELECT SUM(hits) as TotalHits FROM tb_sites WHERE user='$user'") or die(mysql_error()); $row3 = mysql_fetch_array($query3); Quote Link to comment 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.