如何在PHP中传递sql中的变量?

时间:2015-09-25 08:33:26

标签: php mysql

我已经使用此sql查询来获取时间间隔之间的数据

$start_time = $_POST['i_date'].' 07:00:00';
$end_time = $_POST['i_date'].' 14:00:00';
/*echo $start_time;
die();*/
$sql = 'select * from datetime where code = 1001 and time between '.$start_time.' and '.$end_time;

但是我收到以下错误消息: 查询无效:您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在07:00:00和2015-09-09 14:00:00'附近使用正确的语法。在第1行

3 个答案:

答案 0 :(得分:4)

您的查询出错,请尝试此

$sql = "select * from datetime where code = 1001 and time between '".$start_time."' and '".$end_time."'";

答案 1 :(得分:0)

您必须将分号放在您传递的字符串中:

$sql = 'select * from datetime where code = 1001 and time between '.$start_time.' and '.$end_time. ';';

答案 2 :(得分:0)

用单引号将它们绑定:

$sql = "select * from datetime where 
code = 1001 and time between 
'$start_time' and '$end_time'";