Forum Moderators: coopster

Message Too Old, No Replies

Add quantity amounts together

from database field

         

mrnoisy

1:18 am on Jul 30, 2005 (gmt 0)

10+ Year Member



I need to get the total quantity of certain products from a database and add them together. If the total is above 4, they get a bonus product.

It sounds like a simple task but I'm having great difficulty accomplishing it.


$qty = "SELECT quantity FROM shopping WHERE product=('product1'or'product2')and session='$session'";
$qty2=mysql_query($qty) or die(mysql_error());
while($qty3=mysql_fetch_array($qty2))
{
echo $qty3;
}

If there are 2 of 'product1' and 1 of 'product2' it returns 21, how do I add them together to get 3?

dreamcatcher

6:54 am on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use SUM()

$qty = mysql_query("SELECT SUM(quantity) as total FROM shopping WHERE product=('product1'or'product2')and session='$session'") or die(mysql_error());
$row = mysql_fetch_object($qty);

echo $row->total;

dc

mrnoisy

9:35 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



Thanks dc, that worked great. Something else to add to the toolbox.