Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$sth->rows always return zero

Status
Not open for further replies.

Zoom1234

Programmer
Oct 30, 2003
116
BE
Hi,

I am using following code

Code:
$sql = "select * from." ;
$snth = $db->prepare($sql);
my $rcn = $snth->execute();
my $rows = $snth->rows ;
print $rows ;
Even if the query fetches rows and displays it, $rows variable is always set to zero. What am i doing wrong here?
 
because the module doesn't support $snth->rows for a select statement....here is the details on CPAN...
rows
$rv = $sth->rows;
Returns the number of rows affected by the last row affecting command, or -1 if the number of rows is not known or not available.

Generally, you can only rely on a row count after a non-SELECT execute (for some specific operations like UPDATE and DELETE), or after fetching all the rows of a SELECT statement.

For SELECT statements, it is generally not possible to know how many rows will be returned except by fetching them all. Some drivers will return the number of rows the application has fetched so far, but others may return -1 until all rows have been fetched. So use of the rows method or $DBI::rows with SELECT statements is not recommended.

One alternative method to get a row count for a SELECT is to execute a "SELECT COUNT(*) FROM ..." SQL statement with the same "..." as your query and then fetch the row count from that.

I would also NOT recomment doing "SELECT COUNT(*) FROM ..."

instead do "SELECT COUNT(1) FROM ..." it is quicker as it doesn't load all the columns in a table to do the count!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top