in reply to DBI bind error

Binding isn't the problem. Placeholders hold a single value. So if you try to put "1,2" there, the result is:
SELECT value WHERE id IN ('1,2')
which is not what you want.

Try this:

SELECT value WHERE id IN (?, ?)
and use two values.

Oh, and you probably want to supply a table to select from as well.

Replies are listed 'Best First'.
Re^2: DBI bind error
by Nar (Novice) on Jan 18, 2009 at 02:36 UTC
    I see what your saying. THat just brings up a host of new problems though. The input on id is variable and can be changed. Is there a good fix to get around this, or should i just split off the commas and count values then add the ? as a string?
      my @ids = ( 1, 2 ); my $value_lookup_sth = $dbh->prepare(" SELECT value WHERE id IN (" . join(',', ('?') x @ids ) . ") "); $value_lookup_sth->execute(@ids) or debug($DBI::errstr);

      or

      sub plist { '(' . join( ',', ('?') x $_[0] ) . ')' } my @ids = ( 1, 2 ); my $value_lookup_sth = $dbh->prepare(" SELECT value WHERE id IN ".plist(0+@ids)." "); $value_lookup_sth->execute(@ids) or debug($DBI::errstr);

      or if you're particularly silly

      { package My::plist; use Tie::Array; our @ISA = 'Tie::Array'; sub TIEARRAY { bless [], shift } sub FETCH { '(' . join( ',', ('?') x $_[1] ) . ')' } } tie my @plist, 'My::plist'; my @ids = ( 1, 2 ); my $value_lookup_sth = $dbh->prepare(" SELECT value WHERE id IN $plist[@ids] "); $value_lookup_sth->execute(@ids) or debug($DBI::errstr);