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".
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"
?>
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.
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'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Michael701,
That did the trick! Thanks a bunch. Now I've just got to figure out how.
Thanks again!
That did the trick! Thanks a bunch. Now I've just got to figure out how.
Thanks again!
ASKER
If I can get the code, I can break it down from there. Thanks!
Open in new window