how can i display the sum total of one column?

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
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

how can i display the sum total of one column?

Post 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.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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()
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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]}";
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

it's working, isn't it? ;)
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

ya, its working.But is this the right way to do this.Or there is any better method to it?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post 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
User avatar
AnarKy
Forum Contributor
Posts: 119
Joined: Tue Nov 02, 2004 1:49 am
Location: South Africa

ART

Post 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 :)
Post Reply