Page 1 of 1

Displaying the sum of an entire column

Posted: Wed Mar 08, 2006 3:02 pm
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?

Posted: Wed Mar 08, 2006 3:17 pm
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 ;)

Posted: Wed Mar 08, 2006 11:01 pm
by Citizen
Thanks!

Posted: Thu Mar 09, 2006 12:37 am
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;