CustomNet
CustomNet

Reputation: 652

PHP MySQL - SELECT SUM return issue

I am attempting to perform the following query:

SELECT SUM(cash) AS total_cash FROM users

The query should give me back a SUM() of the total cash which I've paid out to users in the form of a field named total_cash.

My HTML/PHP is as follows:

<p class="cash_count">
$<? 
    $total_cash = $db->GetNumRows($db->Query("SELECT SUM(cash) AS total_cash FROM users"));
    echo number_format($total_cash);
?>
</p> paid out!

I know GetNumRows will always show 1 as there's only 1 row, but I don't know what to use instead, can anybody guide me?

Upvotes: 0

Views: 427

Answers (2)

Pablo Martinez
Pablo Martinez

Reputation: 2182

Yes, there's only 1 row, because your query ask for the SUM, and returns only 1 row with the sum.

You can do 2 things, or quit the SUM of the SQL or quit the $db->GetNumRows.

Upvotes: 0

Vishnu
Vishnu

Reputation: 461

You are using some class to connect to database - you have not specified anything about that class. If $db->Query() returns the actual mysql resource, you can do this:

$res=$db->Query("...."); 
$r=mysql_fetch_object($res);
echo $r->total_cash;

Upvotes: 1

Related Questions