1

Just wondering what would be the best way of adding all the values in a column together where the pid=$pid in a column.

Here's some sample code of what I'm using:

$query = mysql_query("SELECT pid, reputation FROM reputation WHERE pid=\"{$post['pid']}\"");

while($rep = mysql_fetch_assoc($query))
{
    echo $rep['reputation'];
}

That works fine, but when more than one row exists where the pid=X I need the reputation column on those rows to add together and output the result.

j0k
  • 22,600
  • 28
  • 79
  • 90
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – j0k Feb 11 '13 at 09:14

2 Answers2

2

use GROUP BY

SELECT pid, SUM(reputation) totalReputation
FROM reputation 
WHERE pid = 0
GROUP BY pid

in php

$query = mysql_query("SELECT pid, SUM(reputation) totalReputation
                      FROM reputation 
                      WHERE pid = " .$post['pid'] ."
                      GROUP BY pid");

and fetch the alias

echo $rep['totalReputation'];

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Thanks @JW. I've sanitized the input earlier on in the script. It's adding the total of the column fine now but it's only showing the first PID, not the others. How can I loop through each one? I'm using: while($rep = mysql_fetch_assoc($query)) { echo $rep['totalReputation']; } – user2059565 Feb 11 '13 at 09:12
  • did you have `WHERE pid = x` in your query? – John Woo Feb 11 '13 at 09:13
  • Yeah, my query is: `SELECT pid, SUM(reputation) totalReputation FROM mybb_reputation WHERE pid = ".$post['pid']." GROUP BY pid` Thanks :) – user2059565 Feb 11 '13 at 09:15
  • try removing the `WHERE` cluase – John Woo Feb 11 '13 at 09:15
  • Works perfectly now. Thanks a load, @JW. :) – user2059565 Feb 11 '13 at 09:18
  • I see you're new to SO. If you feel an answer solved the problem, please mark it as `accepted` by clicking the white check mark (*around 15 mins to an hour later*). This helps keep the focus on older SO questions which still don't have answers. [*How does accepting an answer work?*](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – John Woo Feb 11 '13 at 09:21
1
$query = mysql_query("SELECT SUM(reputation) as rep FROM reputation WHERE pid=\"{$post['pid']}\"");

while($rep = mysql_fetch_assoc($query))
{
    echo $rep['rep'];
}

NOTE: You should not use mysql_* extension since they are now deprecated.

Dr. Dan
  • 2,288
  • 16
  • 19