Page 1 of 1

Query with date returns wrong results

Posted: Wed Jun 09, 2010 9:50 pm
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?

Re: Query with date returns wrong results

Posted: Thu Jun 10, 2010 2:30 am
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

Re: Query with date returns wrong results

Posted: Thu Jun 10, 2010 11:43 am
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.