Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Count Number of rows retrieved from SELECT

by trammell (Priest)
on Jun 13, 2005 at 16:11 UTC ( [id://466204]=note: print w/replies, xml ) Need Help??


in reply to Count Number of rows retrieved from SELECT

Not sure how well this is supported across various databases, but in my version of MySQL, the result of the execute() call returns the record count:
#!perl -l use strict; use warnings; use Data::Dumper; require 'getdbh.pl'; my $dbh = get_dbh(); $dbh->do('USE test'); $dbh->do('CREATE TEMPORARY TABLE t1 (name char(8))'); for (qw/ Jim Judy Steve Jack Jodie Sally Alice /) { $dbh->do('INSERT INTO t1 VALUES (?)', undef, $_); } for (qw/ J S A X /) { my $sth = $dbh->prepare("SELECT name FROM t1 where name like '$_%' +"); my $x = $sth->execute(); print "$_: \$x = $x"; } __END__ J: $x = 4 S: $x = 2 A: $x = 1 X: $x = 0E0
Update: I'm not sure why this keeps getting downvoted. I know the documentation in DBI says that execute() is only guaranteed to return a true value on success, but a little poking around in DBD::mysql makes me think this is a real feature, if a poorly documented one. I'd be grateful if someone would take the time to explain the --'s.

Replies are listed 'Best First'.
Re^2: Count Number of rows retrieved from SELECT
by Akhasha (Scribe) on Jun 14, 2005 at 03:22 UTC
    Without close inspection of the DBD::mysql source and possibly researching the MySQL API, I still wouldn't rely on this. Quite possibly for large queries that don't require sorting, the execute() call could return while at the back end the query is still matching rows.
Re^2: Count Number of rows retrieved from SELECT
by monarch (Priest) on Jun 14, 2005 at 02:16 UTC
    Downvotes can occasionally be a mystery!

    I think your comment on mysql is a good one, although I personally wouldn't rely on poorly documented features if I could avoid it..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://466204]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found