my @userids = ('XD4555', 'JJKKKK'); my @mails; foreach my $userid (@userids) { push @mails, getMails($dbh, $userid); } sub getMails { my ($dbh, $userid) = @_; my $sth = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID WHERE m.USERID_IM = ?"); $sth->execute($userid); my $mails = $sth->fetchall_arrayref(); return @{$mails}; # Caller wants an array, so deref } #### my @userids = ('XD4555', 'JJKKKK'); my $sth = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID WHERE m.USERID_IM IN (?)"); $sth->execute(\@userids); my $mails = $sth->fetchall_arrayref(); #### use strict; use warnings; use Carp; ... my $dbh = DBI->connect(...parameters here...) or croak("$!"); ... my $sth = $dbh->prepare(...statement here...) or croak($dbh->errstr); $sth->execute(...args here...) or croak($dbh->errstr); my $mails = $query->fetchall_arrayref(); $sth->finish; #### if($dbh->do(...somestuff...) && $dbh->do(...otherstuff...)) { $dbh->commit; } else { print STDERR "Database error: ", $dbh->errstr, "\n"; $dbh->rollback; }