I'm having an issue getting a date bound to a variable in an MySql query I'm trying to make.
I have the following query that works
$sql = 'SELECT `date` FROM `sessions` WHERE `date` LIKE "%2022-07-09%"';
I need the LIKE portion to be dynamic, I am trying to select 7 days in the past from the current date, to do that I am creating the date in a PHP variable.
$lastWeek = date("Y-m-d", strtotime("-7 days"));
I replaced the hardcoded value in the LIKE portion of my query
$sql = 'SELECT `date` FROM `lift_sessions` WHERE `date` LIKE "%:lastweek%"';
And then bound the variable to :lastweek
$stmt->bindValue(':lastweek', $lastWeek);
However when I execute that it doesn't match anything
I've tried making the query a few different ways including adding the % in the PHP variable and removing them from the SQL query. It only seems to work if I hardcode the date into the SQL query. I can't figure out what I'm doing wrong, I feel like it's simple and I just can't see it...
Thanks in advance.
public static function compareWeeklyStats() {
$current_date = date('F, jS');
$lastWeek = date("Y-m-d", strtotime("-7 days"));
//error_log($lastWeek);
if(isset($_SESSION['user_id'])) {
$sql = 'SELECT `date` FROM `sessions` WHERE `date` LIKE "%:lastweek%"';
$db = static::getDB();
$stmt = $db->prepare($sql);
//$stmt->bindValue(':user', $_SESSION['user_id']);
//$stmt->bindValue(':lastweek', $lastWeek);
$stmt->bindValue(':lastweek', $lastWeek, PDO::PARAM_STR);
$stmt->execute();
error_log(print_r($stmt->fetchAll(PDO::FETCH_ASSOC), true));
return;
}
return; }