Jump to content

Adding the contents of a Select Query.


allydm

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.
Link to comment
https://forums.phpfreaks.com/topic/19699-adding-the-contents-of-a-select-query/
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]

Archived

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

×
×
  • 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.