How do I add up all values of rows in a table???

Thread Solved

Join Date: Dec 2007
Posts: 136
Reputation: justted is an unknown quantity at this point 
Solved Threads: 2
justted justted is offline Offline
Junior Poster

How do I add up all values of rows in a table???

 
0
  #1
Aug 6th, 2008
Hello,

I was wondering if anyone could help me as I am trying to add all the rows of a particular field in a MySQL table using PHP.

I have a table named members2 which has a field named points! What I want to be able to do is display the total number of points currently in the site that all members have!

But I have no idea how to do this and cannot seem to find anything when googling it!

Any help is much appreciated!

Justin
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 570
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: How do I add up all values of rows in a table???

 
0
  #2
Aug 6th, 2008
you can try something like this:
  1. $query="Select points from members2";
  2. $result=mysql_query($query);
  3.  
  4. $total=0;
  5. while($row=mysql_fetch_array($result))
  6. {
  7. $total=$total + $row['points'];
  8. }
  9.  
  10. echo "Total is:" . $total;
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 16
Reputation: mcd is an unknown quantity at this point 
Solved Threads: 6
mcd mcd is offline Offline
Newbie Poster

Re: How do I add up all values of rows in a table???

 
0
  #3
Aug 6th, 2008
Easier:
  1. $query="SELECT SUM(points) AS `total` from members2";
  2.  
  3. $result=mysql_query($query);
  4. while($row=mysql_fetch_assoc($result))
  5. $total = $row['total'];

If you want to limit the sum to certain sets of data, you will have to use the GROUP BY function in mysql. Learning about grouping and its associated functionality will help you avoid a lot of tedious, repetitive coding.
Last edited by mcd; Aug 6th, 2008 at 11:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 570
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: How do I add up all values of rows in a table???

 
1
  #4
Aug 6th, 2008
Originally Posted by mcd View Post
Easier:
  1. $query="SELECT SUM(points) AS `total` from members2";
  2.  
  3. $result=mysql_query($query);
  4. while($row=mysql_fetch_assoc($result))
  5. $total = $row['total'];

If you want to limit the sum to certain sets of data, you will have to use the GROUP BY function in mysql. Learning about grouping and its associated functionality will help you avoid a lot of tedious, repetitive coding.
much easier:

  1. $query="SELECT SUM(points) AS `total` from members2";
  2. $result=mysql_query($query);
  3. $total=mysql_result($result,0,"total");
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 136
Reputation: justted is an unknown quantity at this point 
Solved Threads: 2
justted justted is offline Offline
Junior Poster

Re: How do I add up all values of rows in a table???

 
0
  #5
Aug 7th, 2008
Thank you so much!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1223 | Replies: 4
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC