Page 1 of 1
mysql date operation problem??
Posted: Mon Jan 15, 2007 4:08 am
by PHPycho
I am trying to display all the news which are up to todays date. I have used following query but didnt get the success
Code: Select all
$sql = "SELECT * FROM news WHERE news_date < = todays_date";
May be i have not enough knowledge about date operation in mysql. What i want is just your help .
Thanks a lot.
Posted: Mon Jan 15, 2007 4:13 am
by Rovas
How did you format the date in the php code and what kind of date format did you put in the MySQL database (i suspect that this is the problem)?
Posted: Mon Jan 15, 2007 4:17 am
by itsmani1
Posted: Mon Jan 15, 2007 4:17 am
by volka
if you copied&pasted
$sql = "SELECT * FROM news WHERE news_date < = todays_date";
from your code then you must have gotten an error message ..if there is any error handling in your script like
Code: Select all
$result = mysql_query($sql, $mysql) or die(mysql_error().': '.$sql);
If there isn't why is there no error handling?

Posted: Mon Jan 15, 2007 6:55 am
by dibyendrah
Code: Select all
$sql = "SELECT * FROM news WHERE news_date < = curdate()';
Posted: Mon Jan 15, 2007 7:35 am
by PHPycho
Actually what i did was
i passed the date as argument
and put it in the query as
Code: Select all
$sql = "SELECT * FROM news WHERE news_date < = $date";
which gave the zero results
But when used curdate() instead of $date gave the required result. why that happened. By the way curdate() and date("Y-m-d') give the current date in Y-m-d format...
Can anybody explain. waitiing for the solution...
Thanks a lot
Posted: Mon Jan 15, 2007 7:42 am
by volka
PHPycho wrote:Code: Select all
$sql = "SELECT * FROM news WHERE news_date < = $date";
The space between < and = causes an sql parse error.
A string representing a date is still a string and tehrefore has to be in quotes for mysql
Code: Select all
SELECT x,y,z FROM abc WHERE datefield <= '2007-01-15'
Posted: Tue Jan 16, 2007 7:10 pm
by PHPycho
Thanks a lot