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

From: patrick keshishian <pkeshish(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: pg_query_params() problem with date_trunc() (possibly other functions as well)
Date: 2014-05-17 04:37:48
Message-ID: CAN0yQBpmW1UeiY7Zp1Yf02K41PpmBiXXNeHTMbDrdPtUpuGZ7Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2014-05-17 04:42:02 Re: pg_query_params() problem with date_trunc() (possibly other functions as well)
Previous Message Adrian Klaver 2014-05-16 19:15:27 Re: Psycopg2 : error message.