while ($ref = $sth->fetchrow_hashref){ ... foreach $col (keys %$ref) {