如何插入诸如"now()-interval'2 minutes'"之类的内容;进入PHP PDO查询? [英] How to insert things like "now() -interval '2 minutes'" into PHP PDO query?

查看:31
本文介绍了如何插入诸如"now()-interval'2 minutes'"之类的内容;进入PHP PDO查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个查询:(在Postgresql 8.4,PHP-fpm 5.3.10(fpm-fcgi)上)

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;

我想使用PHP/PDO查询,所以:

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;

这行不通,我找不到适当的方式来传送会议记录( $ mymin ).如果我对所有时间都进行硬编码,那么代码的另一部分必须正确.

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.

我也尝试过:

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

我已经看到了,没有帮助

推荐答案

时间间隔可以乘以数字.因此,一种解决方法是准备声明为interval '1 minute' * :myminutes的语句,并将"myminutes"参数作为简单的整数传递.

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.

这篇关于如何插入诸如"now()-interval'2 minutes'"之类的内容;进入PHP PDO查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆