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! 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 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. 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. 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? 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 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; ?> 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
Archived
This topic is now archived and is closed to further replies.