dreadiscool
dreadiscool

Reputation: 1598

Count total value in column MySQL & retrieve value in PHP

In PHP, is there a way to get a total of all values in a column? I tried this query

COUNT(player_count) WHERE unique_id = 'test'

Basically, if I have a table like this

+--------------------------+
| player_count | unique_id |
+--------------+-----------+
| 0            | test      |
+--------------+-----------+
| 5            |           |
+--------------+-----------+

I want it the query to return 5.

Also, in a side question, how would you go about doing this in PHP? Normally, to iterate through rows I would do

$result = mysql_query("...");
while ($row = mysql_fetch_array($result) { /* Code */ }

However, I am unsure as how to apply it to a result that should only be a single number.

Would I just do something like

$row = mysql_fetch_array($result);
$total = intval($row["player_count"]);

Thanks for your time, I appreciate it

Upvotes: 0

Views: 191

Answers (1)

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

You have to uso SUM: SUM(player_count) WHERE unique_id = 'test'.

And in your "side question" you're right. Just using mysql_fetch_row for a unique row will work.

Upvotes: 1

Related Questions