Page 1 of 1

how can i display the sum total of one column?

Posted: Wed Aug 17, 2005 5:11 am
by saumya
hi everybody,
how can i get the sum total of a column, in a table from one mysql database?Well my code is

Code: Select all

$totalPrice="SELECT SUM(price) FROM daily";
     print"$totalPrice";
but that simply prints
SELECT SUM(price) FROM daily

Any guidance is appreciated.

Posted: Wed Aug 17, 2005 5:14 am
by Skittlewidth
You haven't actually run the query against the database, all you have done is assigned the query as a string to the variable $totalprice.

Look at mysql_query()

Posted: Wed Aug 17, 2005 5:27 am
by saumya
thanks a lot.
Now i changed my code as follows

Code: Select all

$query="SELECT SUM(price) FROM daily";
     $result=mysql_query($query);
     print"$result";
so it results as

Resource id #5


Any help now, still not getting the value.

Posted: Wed Aug 17, 2005 5:32 am
by patrikG

Posted: Wed Aug 17, 2005 5:51 am
by saumya
well i got the link and tried out and got my result.Thank you very much.But can you please see my code here and tell me wheteher it is the right procedure or not.

Code: Select all

$query="SELECT SUM(price) FROM daily";
     $result=mysql_query($query);
     $myRow=mysql_fetch_array($result);
     print"Total expenditure is  {$myRow[0]}";

Posted: Wed Aug 17, 2005 5:52 am
by patrikG
it's working, isn't it? ;)

Posted: Wed Aug 17, 2005 5:55 am
by saumya
ya, its working.But is this the right way to do this.Or there is any better method to it?

Posted: Wed Aug 17, 2005 6:00 am
by patrikG
saumya wrote:ya, its working.But is this the right way to do this.Or there is any better method to it?
There are always better ways to implement solutions. The question is: where do you draw the line? If the criterium is that it's working: you've achieved that. "Better" as such depends what you define as "better".
If you want to look into more abstract and generic approaches to database-access look into topics like database-wrappers (e.g. adoDB or PEAR::DB), object oriented programming etc.
The point of that would be that you write re-usable code as much as possible, thus reducing the margin for errors considerably, speeding up your work etc. etc. That learning process will take quite some time (and some say, is never finished). Check the "Theory & Design Forum" for some more advanced discussions, which might give you some ideas as to how to improve your code. In book form I'd recommend Harry Fuecks PHP Anthology which offers a good introduction into these topics.

Posted: Wed Aug 17, 2005 6:05 am
by saumya
thanks for your feed back.I will try and learn those too.
now what exactly i want to know is if i want to get a sum of values from one column then all the time i need to access the arry value with 0(zero) index or i can get that value more generic way, some how?
thank you

ART

Posted: Wed Aug 17, 2005 6:15 am
by AnarKy
patrikG wrote: The point of that would be that you write re-usable code as much as possible, thus reducing the margin for errors considerably, speeding up your work etc. etc. That learning process will take quite some time (and some say, is never finished).
I Agree, this is an art and needs time to master...

Once you get the hang of it, you can set up a good repository of useful
snippets, classes, etc.
Makes life simpler :)