kyle.m Posted January 18, 2011 Share Posted January 18, 2011 I have a table that has id and date columns. For whatever reason, the following works: $result = mysql_query("SELECT MAX(id) FROM table WHERE date > " . date("Y-m-d")); $row = mysql_fetch_array($result); $num = $row['MAX(id)']; However, the following does not: $result = mysql_query("SELECT MAX(id) FROM table WHERE date < " . date("Y-m-d")); $row = mysql_fetch_array($result); $num = $row['MAX(id)']; Note that the only change is from using a greater-than operator to a less than operator. I have three entries in the table with the values: 2011-01-17 2011-01-18 2011-01-19 And the date(...) call is returning "2011-01-18". I can't figure out why one query would work while the other does not. This is tested in both the php page and using the SQL tool in phpMyAdmin. Any insight on this would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/224895-where-issues-with-date-field/ Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 What happens if you use the MySQL CURDATE() function instead of concatenating in the php date() function? SELECT MAX(id) FROM comics WHERE date > CURDATE() SELECT MAX(id) FROM comics WHERE date < CURDATE() Quote Link to comment https://forums.phpfreaks.com/topic/224895-where-issues-with-date-field/#findComment-1161600 Share on other sites More sharing options...
kyle.m Posted January 18, 2011 Author Share Posted January 18, 2011 I figured it out, binding quotes around the date field worked. $result = mysql_query("SELECT MAX(id) FROM table WHERE date > \"" . date("Y-m-d") . "\""); Although I'm still not sure why this works with one operator and not the other. Quote Link to comment https://forums.phpfreaks.com/topic/224895-where-issues-with-date-field/#findComment-1161601 Share on other sites More sharing options...
kyle.m Posted January 18, 2011 Author Share Posted January 18, 2011 What happens if you use the MySQL CURDATE() function instead of concatenating in the php date() function? SELECT MAX(id) FROM comics WHERE date > CURDATE() SELECT MAX(id) FROM comics WHERE date < CURDATE() That works as well (and seems to be a more elegant solution than my own). Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/224895-where-issues-with-date-field/#findComment-1161602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.