pg_query_params() problem with date_trunc() (possibly other functions as well)

Поиск
Список
Период
Сортировка
От patrick keshishian
Тема pg_query_params() problem with date_trunc() (possibly other functions as well)
Дата
Msg-id CAN0yQBpmW1UeiY7Zp1Yf02K41PpmBiXXNeHTMbDrdPtUpuGZ7Q@mail.gmail.com
обсуждение исходный текст
Ответы Re: pg_query_params() problem with date_trunc() (possibly other functions as well)
Список pgsql-general
Hi list,

My google skills are very poor, so excuse me if this is
documented some place I'm failing to find.

I started to look into using pg_query_params() with some
php scripts, but ran into this issue where I get:

PHP message: PHP Warning: pg_query_params(): Query failed: ERROR:
invalid input syntax for type timestamp: "$1"
LINE 1: ... date_trunc('day', now()), date_trunc('day', TIMESTAMP '$1')

(excuse typos if any -- I'm hand-typing from a different screen).

The test query is a simple

SELECT date_trunc('day', now()),
    date_trunc('day', TIMESTAMP '2014-05-16 21:00:00');

where the '2014-05-16 21:00:00' is an input parameter.


Trying with pg_query_params() fails as mentioned:

$s = "2014-05-16 21:00:00";
$res = pg_query_params($db,
    "SELECT date_trunc('day', now()), "
    ."date_trunc('day', TIMESTAMP '" . '$1' . "')",
    array($s));

If I remove the single quotes for TIMESTAMP the syntax
error message changes to:

Query failed: ERROR: syntax error at or near "$1"
LINE 1: ... date_trunc('day', now()), date_trunc('day', TIMESTAMP $1)


while the pg_query() method works as expected:

$sql = sprintf("SELECT date_trunc('day', now()), "
    ."date_trunc('day', TIMESTAMP '%s')",
    pg_escape_string($db, $s));
$res = pg_query($db, $sql);


Where is the bug? my code? my understanding of how
this is supposed to work? in pg_query_params()?

Best,
--patrick


В списке pgsql-general по дате отправления:

Предыдущее
От: Adrian Klaver
Дата:
Сообщение: Re: Psycopg2 : error message.
Следующее
От: Tom Lane
Дата:
Сообщение: Re: pg_query_params() problem with date_trunc() (possibly other functions as well)