469,336 Members | 5,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

SUM/Total of values from a DB Query output

I have a database query that outputs values, such as the cost of cars.

car_id car_value

1 20000
2 5000
3 1000
4 40000
and so on....

How do I then write some PHP code, to add all these "car_values" from the result of the query. So if usingt he exampel above, I want to sum up, find the total, 20000+5000+1000+40000=66000

Hope someone can help. My knowledge of PHP is very little.
Mar 13 '09 #1
7 20631

Try the SUM syntax in SQL such as:

Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(car_value) AS total FROM tblcars;
So in PHP:

Expand|Select|Wrap|Line Numbers
  1. $qry = mysql_query(" SELECT SUM(car_value) AS total FROM tblcars ");
  2. $row = mysql_fetch_assoc($qry);
  3. echo $row['total'];
Mar 13 '09 #2
Thanks for the reply. Unforutantely, I want the SUM of the MAXimum values, so essential something like this
SELECT SUM (MAX(car_value))... which obviously wouldn't work.
Hence the reason why I thought first to do the MAX query using MySQL and then use a SUM function of some sort in PHP.

Can someone assist?
Mar 13 '09 #3
6,050 Expert 4TB
What do you mean the MAXimum values? I don't understand.

SUM( ) will return the total of the given column, which is what I thought you wanted?
Mar 13 '09 #4
Just as per my mySQL query, which obviously does not work, I'm aware of the SUM() function in MySQL, however, I don't want just the SUM() of all rows, I need to first get the maximum values from the table and then add them up. I know this would be hard to do in MySQL and would need a double query or something.

It's best to ignore MySQL for now, what I'm after rather is doing SUM using PHP.
So, like my orginal post, sum up, (add up the totals) from a recordset results list. I need to do this in PHP and not MYSQL, as it won't work in MySQL for various reason (especially because I'm need to first retreive the Maximum values because the DB table has foreign key in their linking to another table)

Hope you can help, with how to does this in PHP, adding up the values from a recordset.
Mar 13 '09 #5
6,050 Expert 4TB
Well, I assume you're looping through the results returned through MySQL? Why not just have a variable (set to 0) and then with each loop add to the variable the current rows price.

Expand|Select|Wrap|Line Numbers
  1. $total_price = 0;
  2. while ( $row = mysql_fetch_array( $resource ) )
  3. {
  4.     $total_price += $row['price'];
  5. }
Mar 14 '09 #6
This is what I have and it's not working:

Expand|Select|Wrap|Line Numbers
  1.         <?php do {           $totalBids = 0; ?>
  2.           <?php echo $row_totalBids['highestBids']; ?>
  3.           <?php } while ($row_totalBids = mysql_fetch_assoc($totalBids))
  4.               $totalBids += $row_totalBids['highestBids']?>
Mar 14 '09 #7
6,050 Expert 4TB
Why a do while() loop?

mysql_* functions expect a valid result resource - a value returned via mysql_query(). $totalBids is not one.

Check out a PHP MySQL tutorial.
Mar 14 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by tomektomeknyc | last post: by
7 posts views Thread by Peter Bailey | last post: by
reply views Thread by JM | last post: by
reply views Thread by PW | last post: by
2 posts views Thread by Jerry M. Gartner | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.