Jump to content


Photo

Adding the contents of a Select Query.


  • Please log in to reply
2 replies to this topic

#1 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 07:36 PM

Hi there.

I have a select query

	$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);

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:

		do {
		$total_minus_shipping = array_sum($total);
		}
		while ($total = mysql_fetch_array($total_result));

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.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 04 September 2006 - 07:51 PM

If all you want is the total
$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);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 allydm

allydm
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 04 September 2006 - 07:57 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users