Solved

PHP Sum Column in Table

Posted on 2016-11-11
3
25 Views
Last Modified: 2016-11-14
Can anyone tell me how to sum a column in a table?

I have a table named 'charges' with 2 records.  Record 1 'item_price' is 20.  Record 2 'item_price' is 290.  

I tried this code:

 $qry_charges = $dbh->query("SELECT SUM(item_price) as total_price FROM charges");
			$row = $qry_charges->fetch(PDO::FETCH_ASSOC);
			var_export($row);

Open in new window

and got: array ( 'total_price' => '20', )

Then this code:
	    foreach($qry_counts->query("SELECT SUM(item_price) FROM charges") as $row_sum){
		$totals = $row_sum[0];
		echo $totals;

Open in new window

and got: 20

It's always just getting the first record.  Any ideas on why are appreciated.

Thanks,

Steve
0
Comment
Question by:stkoontz
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
This is a data-dependent problem and we don't have the data.  But your first example looks like the queries I have used for this sort of thing.  Can you please do this for us... Make a SELECT query to get all row values for item_price and print out all the rows.  Please post them in the code snippet here, thanks.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
The query looks correct - can we see your data.
0
 
LVL 2

Author Closing Comment

by:stkoontz
Comment Utility
It was a data problem.  I use Navicat on my local computer to work with the database on my remote server.  When I started up Navicat this morning and opened the table, the second record's item_price was blank.  Navicat must  not have been updating the remote database.

Thanks for the quick help.  I appreciate it.

Steve
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now