简体   繁体   中英

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 !

应该是这样的:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM