Question

I need to pass an array of strings in a parameterized query. I tried the following but it doesn't work at all.

$params = array( array('bob','andrew','larry') );
pg_query_params($conn, 'SELECT * FROM table1 WHERE field = ANY($1)', $params)

I've seen solutions that converts the array to a string and then uses string_to_array to convert the input to an array on the database side, but as those strings are input from users this could end badly, even if I used a delimiter other than a comma. I'd rather use a cleaner solution for this, if possible.

Is there a way to keep this query parameterized and pass an array to it, without resorting to converting the data to a string?

By the way, I'm using PostgreSQL 8.3

Was it helpful?

Solution

pgsql.so is linked to libpq which does not have this capability.

Internally, it would need to serialize an array into string anyway.

libpqtypes has this capability built in, but I'm now aware of any PHP libraries using it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top