Php 为什么它会变成真的?
我似乎找不到问题所在Php 为什么它会变成真的?,php,Php,我似乎找不到问题所在 // Check that someone from this IP didn't register a user within the last hour (DoS prevention) $query = mysqli_query($dbc, "SELECT COUNT(id) FROM accounts WHERE registration_ip = '".$_SERVER['REMOTE_ADDR']."' AND registered > ".(time(
// Check that someone from this IP didn't register a user within the last hour (DoS prevention)
$query = mysqli_query($dbc, "SELECT COUNT(id) FROM accounts WHERE registration_ip = '".$_SERVER['REMOTE_ADDR']."' AND registered > ".(time() - 3600));
if (mysqli_num_rows($query) > 0) {
$errors[] = 'To prevent registration flooding, at least an hour has to pass between registrations from the same IP. Sorry for the inconvenience.';
}
为什么不管发生什么事情,这都是真的?即使accounts表是空的。如果我没有弄错的话,由于您使用的是count,您的数据将始终是一行,表示计数的值。即使有0行符合您的条件,它也会简单地返回此值
+-----------+
| COUNT(id) |
+-----------+
| 0 |
+-----------+
因为你想知道行数。是0。因此有一排
以下是你应该如何处理它
$row = mysqli_fetch_row($query));
$count = intval($row[0]);
mysqli_free_result($query);
if ($count > 0)
....
您正在检查返回的行,而不是值。那就这样检查吧
$row = mysqli_fetch_row($query));
$count = $row[0];
if ($count > 0)
{
}
是的,因此即使计数为0,查询也将返回值为0的计数。这样的计数将返回一行包含计数结果的数据。