return number of rows selected

Hi all, i am doing a perl script to read from a db. I am able to retrieve the rows, but i am unable to return the number of rows selected. i tried

$selectedrows = $sth->numrows;

i got the error msg:
Can't locate object method "numrows" via package "DBI::st"
i changed it to

 $selectedrows = $sth->rows;

i get 0, when i have results returned.

Any advise, thanks.

Which database driver did you use (the one indicated in the connection string)?

Hi Cbkihong!

I am also facing the same error. Can you please provide the information about what is the difference between $sth->numrows and $sth->rows.
Also I want to know if there is some restriction on the use of $sth->rows as I am also getting 0 even when there are rows in the database.

Kindly guide.

regards
Apoorva Kumar

The easiest way to find out how many rows a query will return is to use the COUNT function in SQL.

For operations that aren't queries (such as INSERTs, UPDATEs, and DELETEs), the do method returns the number of rows affected, -1 when it can't determine the right value, or else undef in case of failure.

$rows = $dbh->do("DELETE FROM Conference WHERE Language='REBOL'");
if (! defined $rows) {
  # failed, but this is not needed if RaiseError is active
} else {
  print "Deleted $rows rows\n";
}

Dear Anbu!
Thanks for the quick reply. The statment I am using is a Select one. can I use the do method on select statement also?

regards
Apoorva Kumar

do is used to retrieve row counts for other than select.

For example if

SELECT id,name FROM People WHERE Age > 30

is the query you use then use count to get row count

SELECT COUNT(*) FROM People WHERE Age > 30

No. This is because no resultset is prepared with the do() method. Please refer to the manual page for DBI (http://search.cpan.org/~timb/DBI-1.53/DBI.pm\#do\)

Pls verify below link which is from the CPAN DBI documentation
http://search.cpan.org/~timb/DBI-1.52/DBI.pm\#rows