473,718 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SUM/Total of values from a DB Query output

30 New Member
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 21010
hoopy
88 New Member
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 New Member
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 Recognized Expert Expert
@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 New Member
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 Recognized Expert Expert
@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 New Member
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 Recognized Expert Expert
@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
2225
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 average of all totals,based on many user inputs to txt file?
7
1829
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 16/04/2004 1 19/04/2004 23/04/2004 2 26/04/2004 30/04/2004 1 03/05/2004 07/05/2004 1
0
1488
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 obj_printerq = new ObjectQuery("SELECT * FROM Win32_PrintJob"); TIA!
0
1130
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 ... mySQL = "" mySQL = mySQL & "TRANSFORM Sum(Transactions.Hours) AS SumOfHours " mySQL = mySQL & "SELECT Transactions.Activity" & myActSecond & " as myActivity, Sum(Transactions.Hours) AS myHours " mySQL = mySQL & "FROM Transactions " mySQL = mySQL...
8
4906
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. I have a database with large amounts of historical data for which many queries have been built. If I used Access reports for output, I'd have to build many reports that I don't want to do. For this and other reasons, my approach to inquiries...
2
21056
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 displayed under the relevant category. I can get as far as displaying the data in a nice readable format but I don't know how to have the records placed under their corresponding category. This is my code thus far (sans the html output):
1
5966
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 along the top row and the data items below it. This would give a better appreciation of changes over time. I apologise if I'm missing something obvious. Phil
2
2115
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 this: LastVehicleJobID = DLookup("", "qryGetLastVehicleJobID") qryGetLastVehicleJobID looks something like this: SELECT TOP 1 tblVehicleJobs.VehicleJobID FROM tblVehicleJobs
5
7674
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 columns inside the table have names such as LX1, LX2,....,LX200. If I use DSum for a single (or a few) columns giving the name of the column, everything is OK. I get a total for this (these) ciolumns: Open "N:\FM AM\TEST_LOG1.txt" For Output As...
0
8827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9208
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7991
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6654
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4481
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3182
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2559
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2123
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.