简体   繁体   English

为什么返回true?

[英]Why does it return true?

I can't seem to find the problem. 我似乎找不到问题。

// 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.';
}

Why does this always return true no matter what? 为什么无论如何总是返回true? Even if the accounts table is empty. 即使帐户表为空。

如果我没记错的话,您的数据将始终为1行,表示计数值(因为您使用的是计数)。

Even there is 0 rows that match your condition, it'll simply return this. 即使有0行符合您的条件,它也会简单地返回此行。

+-----------+
| COUNT(id) |
+-----------+
|         0 |
+-----------+

Because you wanted the count of rows. 因为你想要的count行。 Its 0 . 0 Hence there is one row . 因此有一排

Here is how you should handle it. 这是您应该如何处理的。

$row = mysqli_fetch_row($query));
$count = intval($row[0]);
mysqli_free_result($query);

if ($count > 0)
....

You are checking the returned rows not the value. 您正在检查返回的行而不是值。 So check like that 所以像这样检查

$row = mysqli_fetch_row($query));
$count = $row[0];

if ($count > 0)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM