my $command = $sth->fetchrow_arrayref(); if (! $command) { ... "sorry no matches found" } else { do { for (...) { printf ... } } while ($command = $sth->fetchrow_arrayref()); }