基于PHP中的日期选择器动态创建查询

时间:2013-04-15 20:29:06

标签: php sql

我正在尝试在日期选择器中显示用户选择的2个日期之间的某些数据,但我没有找到正确编写查询的方法。总是出现此错误:

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 'BETWEEN2013-04-14AND2013-04-15GROUP BY created_at ORDER BY created_at' at line 1

这是我到目前为止写的:

$sd=$_POST['sd'];
$ed=$_POST['ed'];

$query = mysql_query("SELECT count(name) As Subscribers,CAST(`created_at` AS DATE) As Date FROM gcm_users WHERE created_at BETWEEN".$_POST['sd']."AND".$_POST['ed']."GROUP BY created_at ORDER BY created_at")or die(mysql_error());

1 个答案:

答案 0 :(得分:3)

添加空格:

$query = mysql_query("SELECT count(name) As Subscribers,CAST(`created_at` AS DATE) As Date FROM gcm_users WHERE created_at BETWEEN '".$_POST['sd']."' AND '".$_POST['ed']."' GROUP BY created_at ORDER BY created_at")or die(mysql_error());