Simon
Simon

Reputation: 121

Selecting from a database from Today onwards

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

Answers (5)

Wesley van Opdorp
Wesley van Opdorp

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

gmadd
gmadd

Reputation: 1156

$sql = "SELECT * FROM bs_events WHERE eventDate >= CURDATE() ORDER BY eventDate";

Upvotes: 0

Xand94
Xand94

Reputation: 717

WHERE eventDate >= CURDATE()"; 

should fix you up

Upvotes: 4

DRapp
DRapp

Reputation: 48129

I think you want

where EventDate >= now()

Upvotes: 2

Gowri
Gowri

Reputation: 16835

try this

$sql="SELECT * FROM bs_events WHERE NOW() <=  eventDate   ORDER BY eventDate ";

Upvotes: 1

Related Questions