Jump to content

Archived

This topic is now archived and is closed to further replies.

BadGoat

SUM question **solved**

Recommended Posts

Hello!

I am trying to echo the sum of the records in a table and it returns empty.

[code]
$query = "SELECT SUM(number) AS total FROM records";
            $result = mysql_query($query);
            echo 'the total is:';
            echo $total;[/code]

When I echo the query it appears as above, when I echo the $result it says Resource ID#5. When I echo $total is is empty. What am I doing wrong?

Share this post


Link to post
Share on other sites
$result content is the resource id...you must fetch to get something

and also, php won´t guess what is $total...

try this:
[code]
<?php
$query = "SELECT SUM(number) AS total FROM records";
            $result = mysql_query($query);
            list($total) = mysql_fetch_row($result); // look at php.net to learn more about this
            echo 'the total is:';
            echo $total;
?>
[/code]

Share this post


Link to post
Share on other sites
[code]
$query = "SELECT SUM(number) AS total FROM records";
            $result = mysql_query($query);
            $r = mysql_fetch_array($result);
            echo "total is: {$r['total']}";
[/code]

Share this post


Link to post
Share on other sites
[code]
$query = "SELECT SUM(number) AS total FROM records";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo 'the total is:' . $row['total'];
[/code]

Try that.

EDIT:... bah... beaten to it.

Share this post


Link to post
Share on other sites
Happily I asked a question that three people answered within 10 minutes! Many thanks all!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.