EarthDay Posted November 24, 2022 Share Posted November 24, 2022 (edited) Hi there, I am trying to show entries in my database that have todays date plus seven days. My code is currently $stmt = $pdo->prepare('SELECT * FROM care_plan_review where reminder_date > ? order by id desc'); This does not work sadly. Any thoughts? Cheers, ED. Edited November 24, 2022 by EarthDay Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/ Share on other sites More sharing options...
Barand Posted November 24, 2022 Share Posted November 24, 2022 Try $stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date > CURDATE() + INTERVAL 7 DAY"); 1 Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/#findComment-1602924 Share on other sites More sharing options...
EarthDay Posted November 24, 2022 Author Share Posted November 24, 2022 9 minutes ago, Barand said: Try $stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date > CURDATE() + INTERVAL 7 DAY"); Hi Barand, Thank you for this. I have updated my code and get the following error message; Quote Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /reminders.php:6 Stack trace: #0 /reminders.php(6): PDOStatement->execute() #1 {main} thrown in /reminders.php on line 6 Any ideas? Cheers, ED. Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/#findComment-1602926 Share on other sites More sharing options...
EarthDay Posted November 24, 2022 Author Share Posted November 24, 2022 The full code is $now = date("Y-m-d h:i:sa"); $stmt = $pdo->prepare('SELECT * FROM care_plan_review where reminder_date > ? order by id desc'); $stmt->execute([$now]); $allReview = $stmt->fetchAll(PDO::FETCH_ASSOC); $now = date("Y-m-d h:i:sa"); $stmt = $pdo->prepare('SELECT * FROM care_plan_review where reminder_date > ? order by id desc'); $stmt->execute([$now]); $allReview = $stmt->fetchAll(PDO::FETCH_ASSOC); Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/#findComment-1602927 Share on other sites More sharing options...
ginerjm Posted November 24, 2022 Share Posted November 24, 2022 Why does your code not look like what Barand gave you??? Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/#findComment-1602928 Share on other sites More sharing options...
Barand Posted November 24, 2022 Share Posted November 24, 2022 My query didn't involve any external variables as the date+7 is calculated in the query. I therefore used query() instead of prepare() - there is no execute() required with query(). Incidentally, your code just compares against now, now 7 days from now, Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/#findComment-1602929 Share on other sites More sharing options...
Solution EarthDay Posted November 28, 2022 Author Solution Share Posted November 28, 2022 Hi All, Thank you all for your help on this I have figured this out by using the below code $stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date between now() and now() + INTERVAL 7 DAY"); Cheers, ED. Quote Link to comment https://forums.phpfreaks.com/topic/315579-show-database-entries-by-todays-date-plus-7-days/#findComment-1603001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.