mysql date operation problem??

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

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

mysql date operation problem??

Post 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.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post 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)?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

try

Code: Select all

CURRENT_DATE()
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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? ;)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Code: Select all

$sql = "SELECT * FROM news WHERE news_date < = curdate()';
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Actually what i did was
i passed the date as argument

Code: Select all

$date = date("Y-m-d");
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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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'
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Thanks a lot
Post Reply