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));

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.