Jump to content

PHP/MySQL DATE Query String Issue


northstardomus

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.