Jump to content

SUM question **solved**


BadGoat

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?
Link to comment
https://forums.phpfreaks.com/topic/22024-sum-question-solved/
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]
Link to comment
https://forums.phpfreaks.com/topic/22024-sum-question-solved/#findComment-98485
Share on other sites

Archived

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

×
×
  • Create New...

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.