2015-09-25 26 views
0

我用這個SQL查詢之間的時間間隔來獲取數據如何在PHP中傳遞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服務器版本對應的手冊,以便在第1行'07:00:00和2015-09-09 14:00:00'使用正確的語法。

+0

'... 07:00:00和2015-09-09 14:00:00' - >'... 07:00:00'和'2015-09-09 14:00:00' –

+1

學習使用準備好的語句並綁定變量,那麼你不需要記住引用值 –

回答

4

您的查詢有錯誤請嘗試此操作

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

你必須把字符串中的分號你傳遞:

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

捆成單引號:

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