Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

fetchrow_array return value

by Notromda (Pilgrim)
on Jan 09, 2004 at 23:27 UTC ( #320251=perlquestion: print w/replies, xml ) Need Help??

Notromda has asked for the wisdom of the Perl Monks concerning the following question:

When using DBI, is it safe to do a comparison like:
if (! $sth->fetchrow_array()) { #no rows returned } else { # rows returned }
This feels like an error to me, though I can't prove why. Of course, I would prefer:
if ( $sth->rows() == 0 ) { #no rows returned }
I just would like to know if and why the first might be producing a bug in a program.

Replies are listed 'Best First'.
Re: fetchrow_array return value
by mpeppler (Vicar) on Jan 10, 2004 at 00:05 UTC
    If you use fetchrow_array() like that then if there are one or more rows returned then the first one is lost, because you threw it away in the if().

    Why not do something like:

    my $found = 0; while(my @row = $sth->fetchrow_array) { ++$found; ... process @row ... } if(!$found) { ... no rows found ... }
    As for using $sth->rows(), the problem is that for most database engines the number of rows affected by a SELECT query can only be determined once all of the rows have been fetched...

    Michael

Re: fetchrow_array return value
by exussum0 (Vicar) on Jan 10, 2004 at 00:46 UTC
    my fave is to have an array for results, populate it. If the array has 0 array elements in it, return as if no rows returned, or continue as such.
    my @x = (); while( my @arr = fetchrow_array() ) { push(@x, \@arr ); } if( $#x == .... ) else ....
    Not compilable in this form, but it's an idea.

    Play that funky music white boy..
Re: fethrow_array return value
by zby (Vicar) on Jan 09, 2004 at 23:37 UTC
    I don't see any problem with that code if only you change the plural form in the comments to singular. You fetch there only one row, not an array of rows, but a list representing a row.

    From the DBI documentation:

    If there are no more rows or if an error occurs, then "fetchrow_array" returns an empty list. You should check "$sth->err" afterwards (or use the "RaiseError" attribute) to discover if the empty list returned was due to an error.
    Restating it: the empty list in result means no rows were selected or there was an error.
      In the case in question, there was no need to get the actual data, the only question was if there were any rows returned.

      Also, does an empty list always evaluate to false? I guess it should... I'm just confused because it appears that this evaluation is not always doing what I expect. Maybe the problem lies elsewhere....

        In the case in question, there was no need to get the actual data, the only question was if there were any rows returned.

        In that case you could do with a different approach -

        my $sql = "SELECT count(*) FROM table WHERE .... "; ...

        This query will return a single row containing the count of matching rows (row count) in the table. I believe this is the most efficient and portable way to determine the number of rows, and the fastest too.

        Also, does an empty list always evaluate to false?
        I am delighted at finding myself inconsistent! I feel passionately that "returns an array" is bad documentation, but "returns an empty list" doesn't make me blink twice, even though from a language-semantics point of view, a routine in scalar context cannnot return an empty list, and return () will in fact be an undef result (and hence false).

        Do any others find their brains working this way? Or would everyone else consider "returns an array" and "returns an empty list" equally good or bad?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://320251]
Approved by mpeppler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2019-07-18 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?