I'm sure I'm just formatting this incorrectly but I'm getting a PDO exception with one of my queries and the debug isn't helping.
if I run the following it's works fine :
$db = static::getDB();
$sql = 'SELECT * FROM djs WHERE day = :day
AND start_hour = :hr AND shifts LIKE :shift';
$stmt = $db->prepare($sql);
$stmt->bindParam(':day', $arr['day'], PDO::PARAM_STR);
$stmt->bindParam(':hr', $arr['hr'], PDO::PARAM_INT);
$stmt->bindParam(':shift', $shift, PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_OBJ);
But when I try to add more than one comparison operator in the query like this :
$sql = 'SELECT * FROM djs WHERE day = :day
AND start_hour > :hr AND end_hour <= :hr
AND shifts LIKE :shift';
It throws the following exception pointing to the line containing the execute command :
Uncaught exception: 'PDOException'
Message: 'SQLSTATE[HY093]: Invalid parameter number'
end_hour
is a valid column in the table and I'm trying to ascertain if the star_hour
is greater than :hr
and the end_hour
is less than or equal to :hr
. I must be doing this wrong. Any pointers please? It is because I'm using the same named parameter :hr
in the query twice but only binding it once? If so what is the solution other than setting up another named parameter with the same data?