• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1030
  • Last Modified:

MySQL SUM PHP Query

Hello all,

I've been banging my head against this one and it seems like there should be a simple solution. Unfortunately, it is not presenting itself to me.

My ultimate goal is just to add the Number column from this data:
Name  |      Email                |      Number
test     |      test@test.com      |      2
test     |      test@test.com      |      1
test     |      test@test.com      |      3

The MySQL query "SELECT SUM(number) FROM information" works in phpMyAdmin and the $sql variable is exactly what it spits out for php code, but all I get in return is the "Resource id #3".
<?php
 
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$sql = "SELECT SUM(number) FROM information";
$result = mysql_query($sql);
 
echo "$result"
 
?>

Open in new window

0
ajdrew
Asked:
ajdrew
1 Solution
 
tcs224694Commented:
Try this...
SELECT SUM(number) FROM information group by Name;

Open in new window

0
 
ajdrewAuthor Commented:
tcs224694,

Thanks for the response. Unfortunately, that would work if all the names were the same otherwise it splits the results by name. If you have a bunch of names it doesn't add all the numbers together. It just lists how many people submitted under the same name. phpMyAdmin is the one that returns this output.

I also get the "Resource id #3" on the regular page.

Thanks though.
0
 
aldanchCommented:

OLD
$sql = "SELECT SUM(number) FROM information";
 
NEW
$sql = "SELECT SUM(number) as mysum FROM information";
 
// Your sum is stored in 'mysum'

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Michael701Commented:

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$sql = "SELECT SUM(number) FROM information";
$result_set = mysql_query($sql);
 
$result = mysql_fetch_array($result_set);
 
echo "total is ". $result[0];

Open in new window

0
 
ajdrewAuthor Commented:
Michael701,

That did the trick! Thanks a bunch. Now I've just got to figure out how.

Thanks again!
0
 
ajdrewAuthor Commented:
If I can get the code, I can break it down from there. Thanks!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now