SQL Query with Date Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

SQL Query with Date Function

Post 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 :?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: SQL Query with Date Function

Post by mikosiko »

you must indicate the "unit" for the INTERVAL.... in your case "DAY"

... + INTERVAL 1 DAY
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Re: SQL Query with Date Function

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: SQL Query with Date Function

Post by pickle »

I usually use NOW() rather than CURDATE(). No real reason other than it's 4 characters shorter.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply