Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

The number of rows must be obtained in different ways, depending of the type of SQL statement:

-- Non-SELECT statements

Use the 'rows' method. It returns the number of rows of the last executed command; in case the number of rows is unknown it returns -1.

This method is useful for SQL operations like "INSERT", "UPDATE", "DELETE", etc.

Example:

my $sth = $dbh->prepare("DELETE FROM table WHERE count < '?'");
$sth->execute(25);
 
print "Number of rows deleted: " . $sth->rows;

Please notice that the 'do' method combines 'prepare', 'execute' and 'rows' in a single function, so the above example can be rewritten as:

my $rows = $dbh->do("DELETE FROM table WHERE count < '?'", undef, 25);
 
print "Number of rows deleted: $rows";

-- SELECT statements

In this case the idea is to execute a 'SELECT COUNT(*)' statement using 'selectrow_array' to get the number of rows selected.

Example:

my $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM table WHERE count < '?'", undef, 25);
 
print "Number of rows selected: $rows";