Lubomir Borisov
Lubomir Borisov

Reputation: 168

Mysql select all records that are greater than some date time

I want to select all records from table where date1 is greater than other date2. Date1 is a datetime field in MySQL. Date2 is a datetime object in PHP. I want to compare not only the date but and the time. This is my not working query.

$sql = 'SELECT t.* 
        from transire t 
        where t.returning > '.$transire->returning.';';

Error:

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 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 '' at line 1

Ok I understand, I cant compare php datetime object like that but what is the correct way to do this ? Sure I can use Date(t.returning) but I also need the time in that day. All events after current datetime. I searched and found a few answers but none of them helped me. Thank you !

Upvotes: 0

Views: 551

Answers (2)

vaso123
vaso123

Reputation: 12391

Use double quote for your variable in php, and add a single quote around your date:

$sql = "SELECT t.* from transire t where t.returning > '".$transire->returning."'";

And avoid sql injection by escaping your variable.

Upvotes: 1

Alex Doe
Alex Doe

Reputation: 934

Should be like this:

$sql = 'SELECT t.* from transire t where t.returning > "'.$transire->returning.'";';

Upvotes: 1

Related Questions