Reputation: 121
Im trying to select a bunch of stuff from a database. Each have a date and i'd like to only display the ones whose date is equal to today onwards.
$sql="SELECT * FROM bs_events WHERE NOW() ORDER BY eventDate ";
I have tried looking everywhere and cannot find anything.
Thanks for any help guys
Upvotes: 1
Views: 1809
Reputation: 14941
Important: Using NOW() and CURDATE() does make your query invalid for the query cache. On many request - f.e. the homepage - this could cause performance loss.
I prefer the following snippet:
$oDate = new DateTime();
$sQuery = "
SELECT field1, field2
FROM bs_events
WHERE eventDate >= '".$oDate->format("Y-m-d H:i")."'
ORDER BY eventDate
";
This will achieve your goal, but will cause the query cache to be used for a full minute. Ofcourse you can tweak this to a larger or smaller amount of time.
Upvotes: 0
Reputation: 1156
$sql = "SELECT * FROM bs_events WHERE eventDate >= CURDATE() ORDER BY eventDate";
Upvotes: 0
Reputation: 16835
try this
$sql="SELECT * FROM bs_events WHERE NOW() <= eventDate ORDER BY eventDate ";
Upvotes: 1