Query with date returns wrong results

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
esthermstrom
Forum Newbie
Posts: 9
Joined: Sat Apr 24, 2010 9:59 pm

Query with date returns wrong results

Post by esthermstrom »

I have a query that looks like this:

Code: Select all

$query = "SELECT * FROM events where start_date >=  " . date('Y-m-d') . " order by start_date asc";
When I echo this query, I get what I'd expect:

Code: Select all

SELECT * FROM events where start_date >= 2010-06-10 order by start_date asc
My start_date field is of type date in mySQL, and the return values look like this:

Code: Select all

+------------+-------------------------------------------------------------------------------------------------------------------+
| start_date | subject                                                                                                           |
+------------+-------------------------------------------------------------------------------------------------------------------+
| 2010-06-17 | Monthly Meeting                                                                                                   |
| 2010-01-12 | GSCC Program: "This Old Deed"                                                                                     |
| 2010-02-09 | GSCC, Program: "Mining for Gold on the Internet"                                                                  |
| 2010-03-09 | GSCC, Program: "Timelines: Putting Your Ancestors in Their Place                                                  |
| 2010-01-26 | PCBUG Genealogy SIG, Program: "Writing Your Genealogy Story" |
+------------+-------------------------------------------------------------------------------------------------------------------+
Obviously I need to do something different in my SQL query, since it's returning all records, even when the date is NOT greater than or equal to June 10, 2010. I'm just not sure what to use other than what I'm using now. Any ideas?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Query with date returns wrong results

Post by Eran »

The date needs to be quoted

Code: Select all

SELECT * FROM events where start_date >= '2010-06-10' order by start_date asc
esthermstrom
Forum Newbie
Posts: 9
Joined: Sat Apr 24, 2010 9:59 pm

Re: Query with date returns wrong results

Post by esthermstrom »

Thanks! I've been moving between systems so much that I forget which ones require dates to be quoted and which don't.
Post Reply