Jump to content

Adding results from while loop?


liveandream16

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.