Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Determining if a DBI SELECT comes back empty

by grinder (Bishop)
on Dec 05, 2003 at 21:29 UTC ( #312652=note: print w/replies, xml ) Need Help??

in reply to Determining if a DBI SELECT comes back empty

There are cheaper ways of doing it... When I have to do things like this, the code tends to look like the following:

$sth->execute( @arg ); my $row = 0; while( defined( my $r = $sth->fetchrow_arrayref )) { ... # do stuff with $r .... ++$row; } print "fetched $row row(s)\n";

Just keeping a scalar around isn't a particularly expensive proposition. That's all your mySQL client is doing.

Note that for some database architectures, such as Sybase, the only way to determine the number of rows that a query would return is to go and fetch them; there is no way to know beforehand (that is, after the statement is executed but before the first row is fetched) how many rows will be returned.

I believe that the DBD driver for mySQL supports the rows method, which does provide you with this information beforehand, if you really need it, but do note that using it is not portable.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-01-26 22:25 GMT
Find Nodes?
    Voting Booth?