我已经成功设置了一个 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"> '.$data['gcountry'].' </font></td><td><font color="white">'.$data['dtime'].' </font></td><td><font color="white">'.mysqli_affected_rows($conn).' </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"> '.$data['gcountry'].' </font></td><td><td><font color="white">'.mysqli_affected_rows($conn).' </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/