Entry
Using Perl/DBI how can I check if SELECT returns no rows?
Jan 23rd, 2000 21:52
Nathan Wallace, Matt Wagner, Paul DuBois
Take a look at the DBI docs.
my $query = $dbh->prepare(...);
$query->execute or die "...";
if ($query->rows == 0) {
print "There were no matches.";
}
Actually, the DBI docs say that you can't count on the rows method being
accurate for SELECT statements, and that you should count the rows as
you fetch them.
I often use logic something like this for the case in point:
prepare
execute
count = 0
while (fetch next row succeeds)
{
if count is 0 print any headers that are needed
print row
count
}
finish
if count is 0
print that there were no rows