Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: fethrow_array return value

by zby (Vicar)
on Jan 09, 2004 at 23:37 UTC ( #320252=note: print w/replies, xml ) Need Help??


in reply to fetchrow_array return value

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.

Replies are listed 'Best First'.
Re: Re: fethrow_array return value
by Notromda (Pilgrim) on Jan 10, 2004 at 00:17 UTC
    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.

        and the fastest too.

        Perhaps the most intuitive, but not necessarily the fastest (update: the fastest way to determine if there are any rows, that is, as the OP asked for). If your database is able to return rows as they're found (i.e. it doesn't have to return the entire result set like MySQL), and if there are any rows, it can be faster to do a select 1 from table... than to do a select count(*) from table....

      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: note [id://320252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2019-08-25 00:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?