I've recently learned the importance of prepared statements so I'm embarking on changing all my existing mysqli queries to PDO queries.
I have a table called "people" with fields called name
and dob
(DATETIME), which have data like:
name: Johnny
dob: 2016-12-06 18:30:00
I supply a year in a PHP variable first, for example:
$theyear=2016;
Then, using this example, I'm trying to pull all names for people with a dob in 2016 and echo them, like this below, but it's not displaying any results (and there are plenty):
$stmt = $pdo->prepare("SELECT * FROM people WHERE `dob` BETWEEN ':theyear-01-01' AND ':theyear-12-31'");
$stmt->execute(['theyear' => $theyear]);
while ($row = $stmt->fetch()) {
echo $row['name'] . "<br/>";
}
Being new to PDO, I'm sure I did something stupid/illogical.
Can anybody see my mistake/help me get in the right direction?