简体   繁体   中英

Count of different values in MySQL table not adding correctly

I am trying to get a count of the number of different countries I have in a table but can't get the right result when I echo it out with php. although phpmyadmin gives the correct answer. This is my query:

$sql2 = "SELECT count(*) as country FROM tpf_parks GROUP BY country" ;
$result2 = $pdo->query($sql2);
foreach ($result2 as $row2)

and this is how I am diplaying it

echo $row2[ 'country' ]

On if I run the query on phpmyadmin I get the correct answer of 17. When run through the above php it returns 56. What have I done wrong?

If your question is described correctly and you're looking for the number of different countries you have, the query seems wrong. The query you wrote should give you a row per country, and the value for each row is the number of tpf_parks rows with that country.

The query I would use to determine the number of different countries mentioned in the country column of the tpf_parks table:

SELECT COUNT(DISTINCT country) FROM tpf_parks

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