brooksh Posted June 9, 2009 Share Posted June 9, 2009 So what I'm trying to do is add all the counts up. So let's say you have in the database: title: one count: 3 title: two count: 2 title: one count: 5 title: one should equal 8 for count. $sql="SELECT title,count FROM data WHERE id='$id' GROUP BY title"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { $title=$row["title"]; $count=$row["count"]; $data[$title]=$count; } So something like this: $sql="SELECT title,count FROM data WHERE id='$id' GROUP BY title"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { $title=$row["title"]; $count=$row["count"]; } foreach ($title AS $value) { $data[$title]=$count; } Quote Link to comment https://forums.phpfreaks.com/topic/161473-solved-how-to-count-columns-not-rows/ Share on other sites More sharing options...
sasa Posted June 9, 2009 Share Posted June 9, 2009 SELECT title, SUM(count) as count_sum FROM data WHERE id='$id' GROUP BY title Quote Link to comment https://forums.phpfreaks.com/topic/161473-solved-how-to-count-columns-not-rows/#findComment-852105 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.