stkoontz
asked on
PHP Sum Column in Table
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:
Then this code:
It's always just getting the first record. Any ideas on why are appreciated.
Thanks,
Steve
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);
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;
and got: 20It's always just getting the first record. Any ideas on why are appreciated.
Thanks,
Steve
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The query looks correct - can we see your data.
ASKER
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
Thanks for the quick help. I appreciate it.
Steve