我有一條SELECT語句,我通過PHP和PDO構建,提供已登錄最近XX分鐘的用戶列表。當我硬編碼的時間間隔的SQL語句執行正常,但當我嘗試替換從Web窗體中選擇的時間間隔時,我得到一個SQL錯誤。我不知道什麼是錯的。我使用的PDO和PREPARE語句使用POST數據值作爲參數時發生PDO錯誤
try
{
$sql = 'SELECT DISTINCT PlayerName
FROM Player_Data pd LEFT JOIN character_data cd
ON pd.PlayerUID = cd.PlayerUID
WHERE cd.LastLogin > DATE_SUB(NOW(), :login_interval_value)';
$statement = $pdo->prepare($sql);
$statement->bindValue(':login_interval_value',$_POST['login_interval']);
$statement->execute();
$results = $statement->fetchAll();
}
catch (PDOException $e)
{
$error = 'Error getting player names: ' . $e->getMessage();
include 'error.html.php';
exit();
}
這是我的錯誤...
Error getting player names: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''INTERVAL 60 MINUTES')' at line 4
爲什麼不嘗試存儲'$ _ POST [「login_interval」]'到另一個變量,並將它的參數? –
根據你的建議,我嘗試過,但我最終有同樣的錯誤。 – webworm