472,342 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

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 20802

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

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

Similar topics

by: tomektomeknyc | last post by:
I did an interactive program to input and store employee paycheck information including total net pay into txt file. I can read it from txt file...
by: Peter Bailey | last post by:
I have a query that returns weekly enrollments: Qrygetweeklycountofdosmoduleenrollments Week Commencing Week Ending Total Bookings 02/04/2004...
by: JM | last post by:
How can I store following query output to multi dimensional array or something better? I will be using the output for print queue information...
by: PW | last post by:
I am trying to create a crosstab report in ASP. I'm using ASP/VBScript and Access database. I used Access to create the SQL query, then copied the...
by: GeorgeSmiley | last post by:
Does anyone know of a way, via VBA, to set the screen position of query results to a particular top, left position? I've glanced at API...
by: Jerry M. Gartner | last post by:
Greetings: As evident from a previous post, I am a php noob. I would like to display MySQL query results in an html table with one of the row...
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day...
by: MLH | last post by:
Have a data entry form bound to a table. Has a save button on it that launches DoCmd.RunCommand acCmdSaveRecord in Access 97. In the form...
by: mabrynda | last post by:
Hi, Is there any possibility to sum all the values in many (say n) columns in a single table using VBA in access 2003? Say I have a table with 200...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.