0

我使用 pg_query_params 在 postgres 8.4 和 PHP 5.4.3 中工作。我有一个名为 armies 的表,它有 3 行(仅对于这个例子,它确实有更多)。id 是一个整数,另外两个是没有时区的时间戳。现在 SQL 插入是:

插入 public.armies(id,build_start,build_end) 值 ($1,$2,$3)

我插入的数组是:

    $data['id'] = 1;
    $data['build_start'] = "now()";
    $data['build_end'] = "now() + time '00:00:50'";

错误是 E_WARNING: pg_query_params(): Query failed: ERROR: invalid input syntax for type timestamp: «now() + time '00:00:50'»

当我尝试使用 pg_query 将其作为原始 sql 插入时,它的工作原理如下: INSERT INTO public.armies(id,build_start,build_end) VALUES (11935,now(),now() + time '00:00:50')。

所以我猜它与 pg_query_params() 有关,我真的很想使用 pg_query_params 插入它。

我想要实现的是将 now() 与 00:00:50 秒相加,因此我可以设法更改 $data['build_end']。

4

1 回答 1

0

Please do note that you are passing string literals

$data['build_start'] = "now()";
$data['build_end'] = "now() + time '00:00:50'";

to pg_query_params which cannot prepare a valid date/time value from "now()" or "now() + time '00:00:50'".

Instead of

INSERT INTO public.armies(id,build_start,build_end) VALUES ($1,$2,$3);

change it to

INSERT INTO public.armies(id,build_start) VALUES ($1,now());

and remove the parameters

$data['build_start'] = "now()";
$data['build_end'] = "now() + time '00:00:50'";

Now when the action completes, update the table.

UPDATE public.armies SET build_end=now() WHERE id=$1;

Hope I was able to help you.

于 2013-06-19T17:53:41.407 回答