How to insert things like “now() -interval '2 minutes'” into PHP PDO query?

后端 未结 4 1223
无人共我
无人共我 2021-01-11 20:14

I have a query like this: (on Postgresql 8.4, PHP-fpm 5.3.10 (fpm-fcgi))

select * from users where now() - interval \'2 minutes\' < seenlast ORDER BY seen         


        
4条回答
  •  没有蜡笔的小新
    2021-01-11 20:30

    ... - INTERVAL :myminute MINUTES ...
    

    without the quotes is the proper method. If it helps, think of the placeholders as the equivalent of use old-school variable-based query building methods

    ... - INTERVAL $myminute MINUTES
    

    except the placeholders take care of the injection vulnerabilities that the variables don't. Just because you're using placeholders doesn't mean you can change SQL syntax, so

    ... - INTERVAL '2 minutes'
    ... - INTERVAL ':myminute minute'
    

    are not valid SQL.


    followup for mu:

    mysql> select now() + interval '2 minute';
    ERROR 1064 (42000): 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
    mysql> select now() + interval 2 minute;
    +---------------------------+
    | now() + interval 2 minute |
    +---------------------------+
    | 2013-01-22 13:38:24       |
    +---------------------------+
    1 row in set (0.02 sec)
    

提交回复
热议问题