Re: string = any()

From: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
To: Andy Colson <andy(at)squeakycode(dot)net>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: string = any()
Date: 2012-01-10 21:27:01
Message-ID: CAP_rwwmA153GNysoot7VX1dR-5PWcf6BKP=beD1A22VeP2q5Bw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

W dniu 10 stycznia 2012 16:17 użytkownik Andy Colson
<andy(at)squeakycode(dot)net> napisał:
>> 2012/1/10 Andy Colson<andy(at)squeakycode(dot)net>:
>>> I am writing PHP where it prepares a statement like:
>>> $sql = 'select * from aTable where id = any($1)';
>>>
>>> then in php I create a string:
>>> $args = "{1,2,3}";
>>>
>>> And run it:
>>>
>>> $q = pg_query_params($db, $sql, $args);

> On 1/10/2012 9:11 AM, Filip Rembiałkowski wrote:
>> maybe try to use ARRAY constructor instead?
>>
>> http://www.postgresql.org/docs/9.0/static/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS

>
> As I mentioned, I cannot get it to work:
>
> clayia=# prepare x as select 'bob' = any($1);
> PREPARE
> Time: 0.665 ms
> clayia=# execute x( 'array[''joe'', ''bob'']' );
> ERROR:  array value must start with "{" or dimension information
> LINE 1: execute x( 'array[''joe'', ''bob'']' );
>

no, I meant array constructor with reserved word ARRAY:

prepare x as select 'bob' = any( ARRAY[$2,$3] );
execute x( 'joe', 'bob' );

and in PHP:
<?php
$names = array( "joe", "bob" );
$placeholders = implode( ",", array_map( function($x){return "?"},
$names ) ); #I love Perl
$sql = "select 'bob' = any( ARRAY[$placeholders] )";
$stmt = $dbh->prepare($sql);
$stmt->execute($names);
?>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Yoann MOREAU 2012-01-10 21:44:32 Efficient map looking table
Previous Message David Johnston 2012-01-10 21:10:51 Re: (check) constraints on composite type