Page 1 of 1

SQL Query with Date Function

Posted: Mon Jun 28, 2010 10:49 am
by marnieg
I am trying to select records out of my database using a SQL statement where a date field is one day greater than today

Would this be the correct syntax using the TODAY function and INTERVAL

I'm getting an error trying to fetch these records.

$query = mysql_query("SELECT * FROM courses where course_stat = 'A' and course_stdate = TODAY() + INTERVAL 1");
while ($row = mysql_fetch_array($query)) - get error "supplied argument not valid"

course_stdate is defined as a date type in my database with format yyyy-mm-dd :?

Re: SQL Query with Date Function

Posted: Mon Jun 28, 2010 11:10 am
by mikosiko
you must indicate the "unit" for the INTERVAL.... in your case "DAY"

... + INTERVAL 1 DAY

Re: SQL Query with Date Function

Posted: Mon Jun 28, 2010 12:20 pm
by marnieg
I also had to use CURDATE instead of TODAY using Mysql as database.

$query = mysql_query("SELECT * FROM courses where course_stat = 'A' and course_stdate = CURDATE() + INTERVAL 1 DAY");
while($row = mysql_fetch_array($query))

Dates can be difficult unless you learn all of these related functions.

Re: SQL Query with Date Function

Posted: Tue Jun 29, 2010 10:56 am
by pickle
I usually use NOW() rather than CURDATE(). No real reason other than it's 4 characters shorter.