my $query; foreach $query (@astype) { print $query; my $sql = "select title from $TABLE2 where astype like '%$query'"; my $sth = $dbh->prepare($sql) or die "Cannot Prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); while (@row = $sth->fetchrow_array()) { $adlocation = ( join (",", @row)); } print $adlocation;