php
2012-11-11 187 views 0 likes 
0

我似乎無法找到問題。爲什麼它返回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?即使帳戶表是空的。

+2

這樣的計數返回一行數據,包含計數結果。 –

回答

2

即使有0行匹配您的條件,它會簡單地返回這個。

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

因爲您想要count的行。它的0。因此有一行

以下是您應該如何處理它。

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

if ($count > 0) 
.... 
6

如果我沒有弄錯,您的數據將始終是1行,說明計數值(因爲您使用的是計數)。

+0

是的,所以即使計數爲0,查詢也會返回一個數值爲0的計數。 – Boundless

1

您正在檢查返回的行而不是值。所以像這樣檢查

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

if ($count > 0) 
{ 

} 
相關問題