Jump to content

Archived

This topic is now archived and is closed to further replies.

northstardomus

PHP/MySQL DATE Query String Issue

Recommended Posts

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-07
Or 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

Share this post


Link to post
Share on other sites
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)

:)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.