Jump to content

Archived

This topic is now archived and is closed to further replies.

allydm

Adding the contents of a Select Query.

Recommended Posts

Hi there.

I have a select query

[code]
$total_query = "SELECT `product_price` FROM `robin_basket` WHERE session_id = '".session_id()."'";
$total_result = mysql_query($total_query);
$total = mysql_fetch_assoc($total_result);
[/code]

Now this query returns 3 results (as it should). However i need to add those three results together, and i'm not sure how to do this.

The results are:
105
105
105
So the total of them all should be 315.

I have attempted to solve this problem, by using the following code:

[code]
do {
$total_minus_shipping = array_sum($total);
}
while ($total = mysql_fetch_array($total_result));
[/code]

However, this only adds two results (giving me a total of 210) and not all three added together (which would result in 315).

If someone could explain to me how to get the results to add up correctly i'd be greatful.

Allydm.

Share this post


Link to post
Share on other sites
If all you want is the total
[code]
$total_query = "SELECT SUM(`product_price`)
            FROM `robin_basket`
            WHERE session_id = '".session_id()."'";
$total_result = mysql_query($total_query);
$total = mysql_result($total_result, 0, 0);
[/code]

Share this post


Link to post
Share on other sites
Thankyou!

Works like a charm. Think i'll go check out all the mysql functions now see if there are any more gems to make the coding easier.

Thanks again.

Share this post


Link to post
Share on other sites

×

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.