madjack87 Posted March 7, 2011 Share Posted March 7, 2011 In the database I am storing the date with CURDATE What I need to do is query the database to show any records that are more than 14 days older then todays date. $result = mysql_query("SELECT * FROM customers WHERE stage = 0, TO_DAYS(CURDATE()) - TO_DAYS(date)>14 ORDER BY date"); I know the above code is not correct I just wanted to show everyone what I have been trying. Can I use DATE SUB? Any help would be appreciated. thanks Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/ Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2011 Share Posted March 7, 2011 Yes, DATE_SUB() would probably be best for this. SELECT `field` FROM `table WHERE `date` < DATE_SUB( CURDATE(), INTERVAL 14 DAY ) Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/#findComment-1184055 Share on other sites More sharing options...
madjack87 Posted March 7, 2011 Author Share Posted March 7, 2011 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in This is the error that I am getting. Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/#findComment-1184056 Share on other sites More sharing options...
fenway Posted March 7, 2011 Share Posted March 7, 2011 Then check mysql_error() and see why. Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/#findComment-1184064 Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2011 Share Posted March 7, 2011 I tested that query locally, and the syntax is correct. I assume you edited it to match your table structure, right? Echo the query string, along with mysql_error(), and post those please. $query = "SELECT `field` FROM `table` WHERE `date` < DATE_SUB( CURDATE(), INTERVAL 14 DAY )"; if( !result = mysql_query($query) ) { echo "<br>Query : $query<br>Failed with error: " . mysql_error() . '<br>'; } EDIT: I see where I originally left a closing `backtick` off of the table name. Did you catch that and add it? Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/#findComment-1184066 Share on other sites More sharing options...
madjack87 Posted March 7, 2011 Author Share Posted March 7, 2011 Here is my Error Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' < DATE_SUB( CURDATE(), INTERVAL 14 DAY ) ORDER BY date' at line 1 here is my code $result = mysql_query("SELECT * FROM customers WHERE date' < DATE_SUB( CURDATE(), INTERVAL 14 DAY ) ORDER BY date") or die ('Error: '.mysql_error ()); Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/#findComment-1184238 Share on other sites More sharing options...
madjack87 Posted March 7, 2011 Author Share Posted March 7, 2011 found that i had an extra ' Link to comment https://forums.phpfreaks.com/topic/229889-query-from-the-database-where-date-is-older-than-2-weeks/#findComment-1184241 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.