[英]How to pass date as parameter from php to postgres
i have the following request: 我有以下要求:
select * from newagenda where
debut >'$1' AND debut < date '$1' + interval '24 hours' and
agendaid=$2'
which fails with a type cast error... 因类型转换错误而失败...
failed: the error: = FEHLER: ungültige Eingabesyntax für Typ timestamp:
i try to translate: ERROR: invalid inputsyntax for type timestamp: 我尝试翻译:错误:类型timestamp的inputsyntax无效:
before i parametrized, i had 在我参与之前,我有
select * from newagenda where debut >'$adate' AND debut < date '$adate' +
interval '24 hours' and agendaid=$agid;
which worked perfectly well..... 哪作得很好.....
now, the date comes back through a post request from a web request, so can inherently be manipulated, that's why would like to parametrize it, but i am clueless on how to get this to work..... 现在,日期通过Web请求的发布请求返回,因此可以固有地进行操作,这就是为什么要参数化它,但我对如何使其工作无能为力.....
i tried 我试过了
select * from newagenda where
debut >'$1'::DATE AND debut < date '$1'::DATE + interval '24 hours' and
agendaid=$2'
or 要么
$largs = array($isodate."::DATE");
also 也
$largs = array("'".$isodate."'::DATE");
but nothing worked...... how can i get this get to work? 但是什么都没有起作用……我如何才能使它开始工作? thanks in advance!
提前致谢!
You are probably looking for something along the lines of : 您可能正在寻找以下内容:
<?php
//using single quoates to make sure the variables aren't expanded
$sql = 'select * from newagenda
where
debut >$1::DATE
AND debut < date $2::DATE
and agendaid=$3';
$formattedDate = new \DateTime($date);
$rangeStart = $formattedDate->format('Y-m-d G:i:s');
$formattedDate->add('1 day');
$rangeEnd = $formattedDate->format('Y-m-d G:i:s');
pg_prepare($con,'sel_from_agenda', $sql);
pg_execute($con,'sel_from_agenda', [$rangeStart, $rangeEnd, $agid]);
I using functional connections here but it would probably be best to use OO or PDO. 我在这里使用功能连接,但最好使用OO或PDO。
You can use the pomm-project/foundation
library take advantage of the parameters (and results) converters and by the way use the range type and operators: 您可以使用
pomm-project/foundation
库利用参数(和结果)转换器,顺便使用范围类型和运算符:
$pomm = new PommProject\Foundation\Pomm(['db' => ['dsn' => 'pgsql://user@host/db_name']]);
$date = new \Datetime(); // set your date here
$query = <<<SQL
select * from newagenda
where
tsrange($*::timestamp, $*::timestamp + '1 day'::interval, '()') @> debut
and agendaid = $*
SQL;
$iterator = $pomm['db']
->getQueryManager()
->query($query, [$date, $date, 123]);
foreach ($iterator as $row) {
print_r($row);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.