PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Mon Jan 15, 2007 4:08 am
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.
Rovas
Forum Contributor
Posts: 272 Joined: Mon Aug 21, 2006 7:09 am
Location: Romania
Post
by Rovas » Mon Jan 15, 2007 4:13 am
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)?
itsmani1
Forum Regular
Posts: 791 Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:
Post
by itsmani1 » Mon Jan 15, 2007 4:17 am
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Mon Jan 15, 2007 4:17 am
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?
dibyendrah
Forum Contributor
Posts: 491 Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:
Post
by dibyendrah » Mon Jan 15, 2007 6:55 am
Code: Select all
$sql = "SELECT * FROM news WHERE news_date < = curdate()';
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Mon Jan 15, 2007 7:35 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Mon Jan 15, 2007 7:42 am
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'
PHPycho
Forum Contributor
Posts: 336 Joined: Fri Jan 06, 2006 12:37 pm
Post
by PHPycho » Tue Jan 16, 2007 7:10 pm
Thanks a lot