943,608 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2979
  • PHP RSS
Aug 6th, 2008
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
justted is offline Offline
140 posts
since Dec 2007
Aug 6th, 2008
0

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

you can try something like this:
php Syntax (Toggle Plain Text)
  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;
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Aug 6th, 2008
0

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

Easier:
php Syntax (Toggle Plain Text)
  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.
mcd
Reputation Points: 10
Solved Threads: 6
Newbie Poster
mcd is offline Offline
16 posts
since Jan 2007
Aug 6th, 2008
1

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

Click to Expand / Collapse  Quote originally posted by mcd ...
Easier:
php Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  1. $query="SELECT SUM(points) AS `total` from members2";
  2. $result=mysql_query($query);
  3. $total=mysql_result($result,0,"total");
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Aug 7th, 2008
0

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

Thank you so much!
Reputation Points: 10
Solved Threads: 2
Junior Poster
justted is offline Offline
140 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: simple sql query
Next Thread in PHP Forum Timeline: Allowing one user at a time to access a particular PHP Page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC