查询中的时间戳问题

时间:2012-11-12 22:20:30

标签: php mysql

我有这段代码用于比较时间戳字段和我之前选择的日期! 但我的语法错误.. Howcome?

$query = sprintf( 'SELECT * FROM coupon WHERE date("Y-m-d", "time") = $date' );

错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' "time") = $date' at line 1

同时,我正在比较一个时间戳,即时间戳和一个$date的日期 提前谢谢..

2 个答案:

答案 0 :(得分:1)

time不应引用,因为它是字段引用,而不是string literal

$query = sprintf('SELECT * FROM coupon WHERE DATE(time) = $date');

注意:您容易受到SQL Injection

的影响

答案 1 :(得分:0)

你混淆了PHP函数

date ( string $format [, int $timestamp = time() ] )

和MySQL功能。

DATE( expr )

分别参见手册:

http://php.net/manual/en/function.date.php

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date