user2628641 user2628641 - 7 months ago 23
PHP Question

How to use PHP array in Postgres select

I try to use a php array in a postgres select statement.

I tried:

$sql = "SELECT * FROM some_table WHERE string_field IN ($1) AND other_field = $2;";

$result = pg_query_params($conn, $sql, array(implode(',', $my_arr), $other_field));


But when i run it nothing returns. (when I hardtype everything in postgres, something will be returned)

Answer

Strings needs single quotes as I know. Concat the elements of array like this:

$data = ['a','b','c','d'];
$x = "'" . implode("','", $data) . "'";
var_dump($x);

Result:

'a','b','c','d'