$sth = $dbh->prepare("SELECT * FROM `mml log` WHERE Commands LIKE '%$IMSI%'"); $sth->execute(); my $numFields = $sth->{'NUM_OF_FIELDS'}; while (my $ref = $sth->fetchrow_arrayref){ for (my $i=0; $i<$numFields; $i++){ printf ("%s %s", $i ? "," : "", $$ref[$i]); } }