簡體   English   中英

為什么返回true?

[英]Why does it return true?

我似乎找不到問題。

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

為什么無論如何總是返回true? 即使帳戶表為空。

如果我沒記錯的話,您的數據將始終為1行,表示計數值(因為您使用的是計數)。

即使有0行符合您的條件,它也會簡單地返回此行。

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

因為你想要的count行。 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)
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM