Sujit Libi
Sujit Libi

Reputation: 406

How to pass variable in sql in PHP?

i've used this sql query to get data between the time interval

$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;

but i am getting following error message: Invalid query: 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 '07:00:00 and 2015-09-09 14:00:00' at line 1

Upvotes: 1

Views: 95

Answers (3)

DirtyBit
DirtyBit

Reputation: 16772

Bind them in single quotes:

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

Upvotes: 0

Luiz de Andrade
Luiz de Andrade

Reputation: 33

You must put the semicolon inside the string you're passing:

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

Upvotes: 0

Ankur Tiwari
Ankur Tiwari

Reputation: 2782

Your query has an error try this

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

Upvotes: 4

Related Questions