I have a table of posts, each post has an IP address, I want to see how many times that IP has posted by counting how many times it occurs in the database, and then putting the number of times it's appeared on the screen. Basically this:
MySQL Table:
id entry ip
1 abc 19.123.14.5
2 cde 19.123.14.5
3 efg 12.231.22.9
I want the code to take that, count duplicates, and ouput the count like so:
id entry ip count
1 abc 19.123.14.5 2
2 efg 12.231.22.9 1
Here's my code (that doesn't work) so far:
$result = mysql_query("SELECT ip, entry, id, COUNT(ip) AS A FROM table_name AS C GROUP BY ip HAVING COUNT A > 1 ORDER BY id DESC");
$i = 0;
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$entry = $row['entry'];
$ip = $row['ip'];
$count = ?????;
$i++;
?>
<tr width="100%" align="center">
<td><?php echo $i; ?></td>
<td><?php echo $id; ?></td>
<td><?php echo $entry; ?></td>
<td><?php echo $ip; ?></td>
<td><?php echo $count ?></td>
<td>
<form style="display:inline;" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" value="<?php echo $ip; ?>" name="ip" />
<input type="hidden" value="<?php echo $id; ?>" name="id" />
<input type="submit" value="Ban IP" name="submit" />
</form>
</td>
</tr>
<?php
}
Any help would be much appreciated!
EDIT: Doesn't work: Well firstly, as obvious from my code, the variable $count has nothing assigned to it as I have no idea what to put there. Secondly, I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
$count = $row['A'];should work in your code