manalnor Posted April 7, 2010 Share Posted April 7, 2010 Hello friends, let say we have single database table as following it has id and name and hits i can count id's but how to count hits CREATE TABLE `table` ( `id` bigint(20) unsigned NOT NULL auto_increment, `name` text NOT NULL, `hits` bigint(20) unsigned NOT NULL default '0', PRIMARY KEY (`id`) INSERT INTO `table` VALUES (1, 'one', 5); INSERT INTO `table` VALUES (2, 'two', 3); INSERT INTO `table` VALUES (3, 'three', 6); for ids i use $sql = "SELECT COUNT(id) AS totalid FROM table"; $cux = mysql_query($sql) or die($sql); $counx = mysql_fetch_array($cux); echo $count['totalid']; this will give me ids = 3 Now for hits i don't wanna get how many rows but i wanna calculate it so in this example hits = 5+3+6 = 14 how can i this in that way ! Quote Link to comment https://forums.phpfreaks.com/topic/197895-to-calculate-this/ Share on other sites More sharing options...
premiso Posted April 7, 2010 Share Posted April 7, 2010 $sql = "SELECT SUM(hits) AS totalhits FROM table"; Using MySQL's built in SUM function, this is easily possible. Quote Link to comment https://forums.phpfreaks.com/topic/197895-to-calculate-this/#findComment-1038487 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.