DJSweetness
DJSweetness

Reputation: 153

How do I pass url-array-parameter to 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));

Upvotes: 1

Views: 133

Answers (1)

Blag
Blag

Reputation: 5894

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

Upvotes: 1

Related Questions