my @rows = $sth->fetchrow_array; <--- Reads the first row. You never print it, though. Remove this line. my $rowcount = @rows; <--- Actually, that returns the number of *fields*, since @rows actually contains the first row. Remove this line. if ($rowcount > 0) <--- You can't keep this. You don't even need it. You could reorganize the structure to do something equivalent, but there's no need for that either.