[英]How to pass date as parameter from php to postgres
我有以下要求:
select * from newagenda where
debut >'$1' AND debut < date '$1' + interval '24 hours' and
agendaid=$2'
因類型轉換錯誤而失敗...
failed: the error: = FEHLER: ungültige Eingabesyntax für Typ timestamp:
我嘗試翻譯:錯誤:類型timestamp的inputsyntax無效:
在我參與之前,我有
select * from newagenda where debut >'$adate' AND debut < date '$adate' +
interval '24 hours' and agendaid=$agid;
哪作得很好.....
現在,日期通過Web請求的發布請求返回,因此可以固有地進行操作,這就是為什么要參數化它,但我對如何使其工作無能為力.....
我試過了
select * from newagenda where
debut >'$1'::DATE AND debut < date '$1'::DATE + interval '24 hours' and
agendaid=$2'
要么
$largs = array($isodate."::DATE");
也
$largs = array("'".$isodate."'::DATE");
但是什么都沒有起作用……我如何才能使它開始工作? 提前致謝!
您可能正在尋找以下內容:
<?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]);
我在這里使用功能連接,但最好使用OO或PDO。
您可以使用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.