Link to home
Start Free TrialLog in
Avatar of ajdrew
ajdrew

asked on

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

Avatar of tcs224694
tcs224694
Flag of India image

Try this...
SELECT SUM(number) FROM information group by Name;

Open in new window

Avatar of ajdrew
ajdrew

ASKER

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.

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

ASKER CERTIFIED SOLUTION
Avatar of Michael701
Michael701
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ajdrew

ASKER

Michael701,

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

Thanks again!
Avatar of ajdrew

ASKER

If I can get the code, I can break it down from there. Thanks!