SUM function problem

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
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

SUM function problem

Post by adsegzy »

Hello friends,

am having problem with my SUM function. I tried summing up some figures in a column but is not returning the total of the figures. this is my syntax

Code: Select all

<?php
include("config.php");
$column='amount';
$table='transactions'
$creditbal=mysql_query("SELECT SUM($colunm), FROM $table WHERE status='P'");
echo $creditbal;
?>
but is not echoing any out any, what can i do.

Regards,
adsegzy
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: SUM function problem

Post by jraede »

You spelled "column" two different ways...is that the problem?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: SUM function problem

Post by jraede »

Oh, and also you should look up what mysql_query does...it doesn't return a string that you can just echo. You have to retrieve the data somehow with mysql_fetch_array, mysql_fetch_row, etc, depending upon how you want to work with it.
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

Re: SUM function problem

Post by adsegzy »

Well i have corrected the colunm error and have rewritten it as below but it's not still echoing out my sum

Code: Select all

<?php
include("config.php");
$column='amount';
$table='transactions'
$creditbal=mysql_query("SELECT SUM($column), FROM $table WHERE status='P'");
$rows=mysql_fetch_array($creditbal);
echo $rows[$creditbal];
?>
but still not working. any other way out?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: SUM function problem

Post by Eran »

Are you a programmer? $rows is an array, you can't echo it.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: SUM function problem

Post by jraede »

Without looking it up, I'm also fairly certain that SUM is a group function, in which case you would need GROUP BY syntax at the end of your query. So in this case it would be "GROUP BY `status`".

And you are trying to access the value in the $rows array with the key that corresponds to your query object?? That makes no sense...in this case, since you're not giving SUM($column) a name, you'd have to $rows[0] to get the first returned value, which is the result of the SUM function.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: SUM function problem

Post by Eran »

You can use SUM() without a GROUP BY modifier
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: SUM function problem

Post by superdezign »

Am I the only one suspicious about the comma after the SUM() function? Have you tried echoing mysql_error(), yet?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: SUM function problem

Post by Eran »

Yes, that comma shouldn't be there
Post Reply