dongqian9567 2017-04-12 18:52
浏览 49
已采纳

如何将url-array-parameter传递给pg_execute

I'm sending my parameters using an URL like test.php?id=2,4,5,6,7

And use the following php :

$id_array = $_GET['id'];
$id_array = explode(',', $id_array);

pg_prepare($conn, 'test', 'select 1 from my_test_table where id = ANY ($1)');
pg_execute($conn, 'test', array($id_array);

I keep getting things like

malformed array literal: "["73","123","412"]"

and similar errors. I can't figure out the proper way to pass an array to my pg_execute, but I need to make sure all ids are in the db first before doing an insert.

Specifically, I'm trying to do an insert into an int array in the postgres db.

Can anyone help with this?

I've also tried

json_encode(explode(',', $id_array));
  • 写回答

1条回答 默认 最新

  • douping3427 2017-04-12 19:43
    关注

    Just this should do it :

    pg_prepare($conn, 'test', 
        " SELECT 1 
        FROM my_test_table 
        WHERE id = ANY (string_to_array($1, ',')) ");
    pg_execute($conn, 'test', array($_GET['id']));
    

    We use the postgresql string_to_array function, as on the top answer there https://stackoverflow.com/a/36930781/5546267

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘