PHP MYSQL : Return rows for each unique entry

标签 php mysql

我已经成功设置了一个 PHP 脚本,可以通过地理位置记录我网站上的访问者。 我正在尝试编辑代码以得出每个国家/地区的点击次数,例如美国:14;国标:2;等等... 这是在每列上返回“132”的代码,因此每个国家/地区没有唯一的匹配结果。

$squery = 'SELECT `gcountry`, `dtime` FROM `allowed`'; 
$ress = mysqli_query($conn, $squery);
echo '<style>.tables {margin-left: 6cm;}</style>';  
echo '<table border="1" class="tables">';
echo'<th><font color="white">Country</font></th><th><font color="white">Date / Time</font></th><font color="white">Hits</font></th>';

while($data = mysqli_fetch_array($ress))
{
echo '<tr>';
echo '<td><font color="white">&nbsp;'.$data['gcountry'].'&nbsp;</font></td><td><font color="white">'.$data['dtime'].'&nbsp;</font></td><td><font color="white">'.mysqli_affected_rows($conn).'&nbsp;</font></td>';
echo '</tr>';
}

编辑了帖子,所以... 这是代码,并附加表格条目:

$squery = 'SELECT gcountry,COUNT(*) FROM allowed GROUP BY gcountry';
$ress = mysqli_query($conn, $squery);

echo '<style>.tables {margin-left: 6cm;}</style>';
echo '<table border="1" class="tables">'; echo'<th><font color="white">Country</font></th><font color="white">Hits</font></th>'; 

while ($data = mysqli_fetch_array($ress)) {
    echo'<tr>';
    echo '<td><font color="white">&nbsp;'.$data['gcountry'].'&nbsp;</font></td><td><td><font color="white">'.mysqli_affected_rows($conn).'&nbsp;</font></td>';
    echo'</tr>';
}

表格内容:

IP               |  UserAgent                 |       GCOUNTRY             |    GCITY                   | DTIME
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          GB                | London                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RU                | Moscow                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          GB                | London                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          GB                | London                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Lombard                    | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | San Antonio                | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Constanta                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Bucharest                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Iasi                       | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          NL                | Amsterdam                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Fort Lauderdale            | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Constanta                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Constanta                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RO                | Bucharest                  | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          RU                | Moscow                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Irving                     | 21-08-2016
xxx.xxx.xxx.xxx  |Mozilla Firefox ....        |          US                | Anaheim                    | 21-08-2016

最佳答案

看看the COUNT function ,以及如何将其与 GROUP BY 一起使用:

Earlier, you retrieved the names of the people who owned pets. You can use COUNT() if you want to find out how many pets each owner has:

mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;

+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  |        2 |
| Diane  |        2 |
| Gwen   |        3 |
| Harold |        2 |
+--------+----------+

关于PHP MYSQL : Return rows for each unique entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39065067/

相关文章:

php - 使用apache更改php程序中的用户

MySQL将一列的值复制到另一列

php - mysql PHP 查询数组

javascript - Woocommerce wc-ajax 返回 html 响应

php - MySql & PHP - 如何将字符串转换为 DATETIME

php - Python 3.6 Bittrex API 无效签名

mysql - 如何选择最后一天插入的所有行?

php - 实现评级系统的概念性帮助,让项目按时间减少

java - 如何在使用 spring jdbcTemplate 发出 REST 更新请求时验证记录是否存在?

php、mysql、打开数据库花费太长时间