skip to Main Content

Struggling a while with passing an array parameter for a simple Redshift (Postgres8) query like this:

// Example: 
$sql = "select * from country where country_code IN ($1) LIMIT 10";
$params = ["'GB','US'"];

pg_send_query_params($conn, $sql, $params);

Redshift engine does not care about any "array" format I’m trying to pass
the same happens for integer[]

I’ve been trying passings parameters in a various ways like

"'GB','US'"
"{'GB','US'}"
"['GB','US']"
"('GB','US')"
"ARRAY['GB','US']"
// etc...

I’ve been also trying with cast operator for the parameter like
IN ($1::text[])

None of those work as expected

2

Answers


  1. Chosen as BEST ANSWER

    Thanks @FrankHeikens and @Stefanov.sm This works for Redshift

    $sql = 'select * from country where country_code = any($1) LIMIT 10';
    $params = ['{GB,US}'];
    

    Thanks!


  2. As @FrankHeikens commented, use =any().

    $sql = 'select * from country where country_code = any($1::text[]) LIMIT 10';
    $params = ['{GB,US}'];
    

    or

    $sql = 'select * from country where country_code = any(('{'||$1||'}')::text[]) LIMIT 10';
    $params = ['GB,US'];
    

    Unrelated but it is highly recommended to use PDO instead of pg_* set of functions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search