How do I know how many rows a Perl DBI query returns?

The DBD::mysql driver has a the rows() method that can return the count of the results:

$sth = $dbh->prepare( ... );
$sth->execute;
$rows = $sth->rows;

This is database-driver specific, so it might not work in other drivers, or it might work differently in other drivers.


my $th = $dbh->prepare(qq{SELECT bi_exim_id FROM bounce_info WHERE bi_exim_id = '$exid'});
$th->execute();
my $found = 0;
while ($th->fetch()) 
{
   $found = 1;
}

Your query won't return anything if the row doesn't exist, so you can't de-reference the fetch.

Update: you might want to re-write that as

my $found = $th->fetch();

Tags:

Mysql

Perl

Dbi