1

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));
Blag
  • 5,818
  • 2
  • 22
  • 45
DJSweetness
  • 153
  • 1
  • 14

1 Answers1

1

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

Community
  • 1
  • 1
Blag
  • 5,818
  • 2
  • 22
  • 45
  • 1
    The only change I made is that id is an int so I casted the function like so: string_to_array($1, ',')::int[] . Other than that, that's exactly what I was looking for. Thank you! – DJSweetness Apr 12 '17 at 19:54