liveandream16 Posted March 18, 2011 Share Posted March 18, 2011 Hello, I have a database that contains a column with a int value for number of slots reserved, and what I want to do is fetch the value of each column and add them together to create one whole number. For example, in my database under preserved I have two separate values of 2 and 4.. I would like to take those two values and add them up into 6 through php... here is what I have so far: $slots = mysql_query("SELECT * FROM treservations WHERE reservation_date='$reservation_date' AND reservation_hour='$reservation_hour'") or die(mysql_error()); while ($row = mysql_fetch_assoc($slots)) { $row["preserved"]; } mysql_free_result($slots); Any help would be GREATLY appreciated.. if I need to add additional code please let me know. Thank you! Link to comment https://forums.phpfreaks.com/topic/230977-adding-results-from-while-loop/ Share on other sites More sharing options...
liveandream16 Posted March 18, 2011 Author Share Posted March 18, 2011 I have figured it out!! I need to SUM it up before it comes out of the database... here is my end result: $slots = mysql_query("SELECT SUM(preserved) FROM treservations WHERE reservation_date='$reservation_date' AND reservation_hour='$reservation_hour'") or die(mysql_error()); while($row = mysql_fetch_array($slots)){ echo "Total ". $row['SUM(preserved)']; echo "<br />"; } Thank you to anyone who considered answering... Link to comment https://forums.phpfreaks.com/topic/230977-adding-results-from-while-loop/#findComment-1188975 Share on other sites More sharing options...
.josh Posted March 18, 2011 Share Posted March 18, 2011 Can clean that code up a bit... $slots = mysql_query("SELECT SUM(preserved) FROM treservations WHERE reservation_date='$reservation_date' AND reservation_hour='$reservation_hour' LIMIT 1") or die(mysql_error()); $total = mysql_result($slots,0); echo $total; Link to comment https://forums.phpfreaks.com/topic/230977-adding-results-from-while-loop/#findComment-1188982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.