简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM