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

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

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误