[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Double MySQL query thru DBI.


On 12/7/05, Sean Davis <suppressed> wrote:
> Select id from table where name in ('q','w');

Yes, that will technically work. However, how does your solution tell
me which id corresponds to q and which to w?

Much better would be:
SELECT name, id
  FROM table
 WHERE name IN ( 'q', 'w' )

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;
}

This will return something that looks like:
$results = [
    { id => 3, name => 'w' },
    { id => 5, name => 'q' },
];

Rob

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.