1

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)

3

1 Answer 1

1

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'
3
  • anyway, first let's check, are there values in your array! Commented May 2, 2016 at 14:35
  • Until there is a quote in your array value strings… Commented May 4, 2016 at 12:51
  • OP should handle this in $my_arr with an array_map for escape. I just told him how to join the pieces for strings. Commented May 4, 2016 at 13:01

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.