my $lastid = 0; while ($sth->fetch) { print $row; if ($lastid != $id) { print ""; } }