Displaying the sum of an entire column

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Displaying the sum of an entire column

Post by Citizen »

My code looks like this:

Code: Select all

$sql = "SELECT SUM(`totalgiven`) FROM `accounts`";
$result=mysql_query($sql);
$allgiven = $result;
echo $allgiven;
The echo'd result is this:

Resource id #4

What am I doing wrong?
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Change it to this...

Code: Select all

$sql = "SELECT SUM(`totalgiven`) AS totalgiven FROM `accounts`";
$result=mysql_query($sql);
$allgiven = mysql_fetch_array($result);

echo {$allgiven['totalgiven']};
You needed to extract the rows first ;)
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Thanks!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

no need to fetch a row, you only want one result :-P

Code: Select all

$allgiven = mysql_result($result,0);
echo $allgiven;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply