I have a SELECT statement that I am building via PHP and PDO to provide a list of users who have logged in the last XX minutes. When I hard code the interval of time the SQL statement executes fine yet when I try to substitute an interval selected from a web form I get a SQL error. I am not sure what is wrong. I am using PDO and the PREPARE statement
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();
}
This is the error I get ...
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