473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

SUM/Total of values from a DB Query output

30
Hi,
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 20944
hoopy
88
Hi,

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'];
Cheers.
Mar 13 '09 #2
cptuser
30
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
Markus
6,050 Expert 4TB
@cptuser
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
cptuser
30
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
Markus
6,050 Expert 4TB
@cptuser
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. }
  6.  
Mar 14 '09 #6
cptuser
30
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
Markus
6,050 Expert 4TB
@cptuser
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

1
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 but how can i lets say modify it to calculate the...
7
by: Peter Bailey | last post by:
I have a query that returns weekly enrollments: Qrygetweeklycountofdosmoduleenrollments Week Commencing Week Ending Total Bookings 02/04/2004 02/04/2004 0 05/04/2004 09/04/2004 0 12/04/2004...
0
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 reference later in program. ObjectQuery...
0
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 SQL and embedded it in my ASP like this ... ...
8
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 techniques but cannot find exactly what will do the trick....
2
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 values as a category and the rest of the data...
1
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 and display it in a query with each day's date...
2
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 after-update code, I have a line looking something like...
5
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 columns. Say the table name is TableCOUNT and the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.