Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I would like to use PHP/PDO query, so:

$mymin=5; //this is a variable can be changed by $_GET
$query = $db_conn->prepare("select * from users where now() - interval ':myminute minutes' < seenlast ORDER BY seenlast"); 
$query->bindParm(":myminute",$mymin)
$query->execute;

This does't work, I cant find a way to pass the minutes($mymin) in a proper way. If I hardcode the timestuff everithing works, so the other part of the code must be correct.

I also tried:

$temp=$mymin." minutes";
$query = $db_conn->prepare("select * from users where now() - interval :myminute < seenlast ORDER BY seenlast"); 
$query->bindParm(":myminute",$temp)

I already saw this, didn't help

like image 222
ykat Avatar asked Jan 22 '13 18:01

ykat


1 Answers

Intervals can be multiplied by numbers. So one approach to this is to prepare the statement saying interval '1 minute' * :myminutes instead, passing the "myminutes" parameter as a simple integer.

like image 157
araqnid Avatar answered Sep 18 '22 10:09

araqnid