majhate Posted January 13, 2008 Share Posted January 13, 2008 Hello, Lets say I have a table with the fields: ID, Color, Value So.. ID - Color - Value 1 - Blue - 3 2 - Orange - 1 3 - Black - 4 4 - Red - 2 How would I get the total of the values in one varible. so $total would = 10 I've been trying loops, but I cannot get this to work. Any sugguestions? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/ Share on other sites More sharing options...
joecooper Posted January 13, 2008 Share Posted January 13, 2008 post up what you have tried, may help us a bit Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/#findComment-438211 Share on other sites More sharing options...
majhate Posted January 13, 2008 Author Share Posted January 13, 2008 This is what I have tried: $query = "SELECT choice_count FROM colors WHERE ID='$ID'"; $result = mysql_query($query); while($rows = mysql_fetch_array($result, MYSQL_NUM)) { $value = $rows[0]; echo $new . "<br />\n"; } This will only display the values with a line break, I don't know how to add them up. I am not even sure if this would be the right way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/#findComment-438216 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 Just use the mysql SUM function. Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/#findComment-438233 Share on other sites More sharing options...
majhate Posted January 13, 2008 Author Share Posted January 13, 2008 Could you elaborate on that please? Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/#findComment-438291 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 http://www.tizag.com/mysqlTutorial/mysqlsum.php Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/#findComment-438295 Share on other sites More sharing options...
GingerRobot Posted January 13, 2008 Share Posted January 13, 2008 As in: <?php $sql = mysql_query("SELECT SUM(value) FROM colors") or die(mysql_error()); $sum = mysql_result($sql,0); echo $sum; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85851-solved-get-all-values-in-column/#findComment-438304 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.