使用 INSERT ON CONFLICT 和几何图形的 Postgres 参数化查询在 PHP 中不起作用

问题描述 投票:0回答:1

我不知道为什么这不起作用。是否与在查询中使用参数两次有关?使用 PHP 7.4.33、Postgres PostgreSQL 9.6.22。

错误是:

Warning: pg_query_params(): Query failed: ERROR:  inconsistent types deduced for parameter $2
LINE 11:                 , $2
                           ^
DETAIL:  double precision versus numeric in /home/douglass/redata/process.php on line 36

可能是什么问题?

这就是我所做的(我知道纬度/经度在行中两次,一次在一个点中,一次在单独的列中,但为了示例,我想了解错误):

创建的表:

(
  unique_id numeric,
  latitude numeric,
  longitude numeric,
  coordinates geometry(Point,4326)
);

运行 PHP:

    pg_query_params($connection, "
INSERT INTO test_table5
                (
                unique_id 
                , latitude
                , longitude
                , coordinates )
        VALUES (
        $1
                , $2
                , $3
                ,  ST_SetSRID(ST_MakePoint( $3, $2), 4326) )
        ON CONFLICT
                (unique_id)
        DO UPDATE SET
                latitude = $2
                , longitude = $3
                , coordinates =  ST_SetSRID(ST_MakePoint( $3, $2), 4326)",
                     [
                        50113947,
                        35.76673,
                        -78.36914
                     ]);
php sql postgresql prepared-statement postgres-9.6
1个回答
0
投票

听起来像是 Postgres 在抱怨

double precision versus numeric
。鉴于您的列是
numeric
,PHP 绑定列必须是
double precision
。当您在 SQL 中使用显式强制转换时会发生什么?

$2::numeric

查看 PHP 文档,PDO 中没有 Postgres 的

numeric
类型的表示。

© www.soinside.com 2019 - 2024. All rights reserved.