northstardomus Posted September 8, 2006 Share Posted September 8, 2006 I looked for the newbie board, couldn't find it because I'm new. Sorry!Anyway,I'm trying to query a MySQL table that has a field in date format where the dates are formatted like 2006-09-07.I want to store the interesting dates in a PHP variable that could come from a URL, i.e. http://www.mydomain.com/index.php?date=2006-09-07Or the date could come on a query of my date field. Regardless, it will eventually be assigned to a php variable.Anyway, what's seems to be happening is MySQL is assuming I'm doing some arithmatic and is processing the 2006-09-07 to be the value 2006 minus 9 minus 7 = 1990.---------------------------------------------------------------------------------------------------So, if I hard code the interesting date, I get what I want:<?php$sql="SELECT * FROM table where date <= '2006-09-07'";?>This will get me everything in my table that has a date less than or equal to '2006-09-07', exactly what I want.------------------------------------------------------------------------------------------------------But if I use the same query using a php variable like below:<?php$date = '2006-09-07';$sql="SELECT * FROM table where date <= $date";?>This will get me everything in my table that has a date less than or equal to '1990' or 2006-9-7 or the equivalent of these queries:<?php$sql="SELECT * FROM table where date <= 2006-09-07";?><?php$sql="SELECT * FROM table where date <= 1990";?>------------------------------------------------------------------------------------------So, it seems to me that MySQL is not seeing my PHP variable as a string, I'm not sure.I tried a few things like CONCAT() and CAST() but they didn't seem to work.Can anybody tell me where I'm going wrong?Thanks,NSD Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 8, 2006 Share Posted September 8, 2006 need to surround the value with single quotes like so:[code]$sql="SELECT * FROM table where date <= '$date'";[/code]otherwise it thinks you are looking for 1990 (2006 - 7 - 9 = 1990):) Quote Link to comment Share on other sites More sharing options...
northstardomus Posted September 8, 2006 Author Share Posted September 8, 2006 OK, that works, thanks!!! Quote Link to comment 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.