> -----Original Message-----
> From: Rob Kinyon [mailto:[EMAIL PROTECTED]
> Sent: Thursday, December 08, 2005 4:53 PM
> To: Sean Davis
> Cc: Strong; [email protected]
> Subject: Re: [cgiapp] Double MySQL query thru DBI.
> [...]
> The Perl for this could look something like:
>
> sub get_name_ids {
> my @names = @_;
>
> my $sql = 'SELECT name, id FROM table WHERE name IN (';
> $sql .= join( ',', ('?') x @names );
> $sql .= ')';
>
> my $sth = $dbh->prepare( $sql );
> $sth->execute( @names );
>
> my $results = $sth->fetchall_hashref;
>
> return $results;
> }
I would suggest to use SQL::Abstract here: it permits to avoid the SQL
contamination into your Perl code:
sub get_name_ids {
my ($dbh, $names) = @_;
require SQL::Abstract;
my $s = SQL::Abstract->new;
my ($sql, @bind)
= $s->select('table', ['name', 'id'], { name => {-in => $names} });
return $dbh->selectall_arrayref($sql, { Slice => {} }, @bind)
# or
# return $dbh->selectcol_arrayref($sql, { Columns=>[1,2] }, @bind)
# as suggested by Cees.
}
(With respect to the original message, it is assumed $names = [EMAIL
PROTECTED]).
> This will return something that looks like:
> $results = [
> { id => 3, name => 'w' },
> { id => 5, name => 'q' },
> ];
Ditto ( if you opt for selectall_arrayref() ).
Ciao,
Emanuele.
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]