开发者

sql query with condition on date

I have a little simple problem. I want to perform a query on a Mysql table to select posts posted after a certain date but I don't manage to get it working by setting that date as a PHP variable.

The following query doesn't return anything:

$reqsql = "SELECT * FROM posts WHERE post_date <= " . $from_date;

The "post_date" field is set as a datetime. I have tried everything for the $from_date PHP variable format:

$from_date = date();
$from_date = date('Y-m-d-H开发者_开发百科-i-s');
$from_date = date('Y-m-d H:i:s');
$from_date = strtodate("now");

None of these work...

But it works fine if I put a basic Mysql date like:

$reqsql = "SELECT * FROM posts WHERE post_date <= NOW()";

Any idea? Thanks!


You probably only need to wrap $from_date in single quotes.

$reqsql = "SELECT * FROM posts WHERE post_date <= '" . $from_date ."'";


Put the date between quotes:

$reqsql = "SELECT * FROM posts WHERE post_date <= '" . $from_date . "'";


You need to use the date like any other string in your query:

$reqsql = "SELECT * FROM posts WHERE post_date <= '" . $from_date . "'";


You could pass the date from PHP in UNIX-time format using time() and then run FROM_UNIXTIME() on it in the SQL query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜