Solved

PHP Sum Column in Table

Posted on 2016-11-11
3
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41884327
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 58

Expert Comment

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

Author Closing Comment

by:stkoontz
ID: 41886233
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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