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

no rows returned for dbi fetchall_arrayref

by essej1 (Novice)
on May 07, 2013 at 12:20 UTC ( #1032504=perlquestion: print w/replies, xml ) Need Help??
essej1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the DBI fetchall_arrayref to get the rows from an Oracle SQL statement. (I'm using this one as I do not expect many hits.) There are valid conditions when now rows are found.

my $result_array = $sth->fetchall_arrayref; foreach my $row (@{$result_array}) { print join (",", @$row),"\n"; print FH join (",", @$row),"\n"; }
If I put
print "==>@{$result_array}<==\n";
after the fetchall I get "==><==" which indicates an empty string. Testing for this with the following doesn’t seem to work.
if (@{$result_array} eq “”) { <some message> } else { <do the output> }
How do I test for the "nothing here" condition.
Paradise: Florida, full tank of gas, no appointments.

Replies are listed 'Best First'.
Re: no rows returned for dbi fetchall_arrayref
by toolic (Bishop) on May 07, 2013 at 12:30 UTC
    This should suffice:
    if ($result_array) { print "something\n"; } else { print "nothing\n"; }

    Also, the DBI POD mentions checking $sth->err

      Thanks toolic, exactly what I needed. I had the DOH moment just as I opened your response.
      Paradise: Florida, full tank of gas, no appointments.
Re: no rows returned for dbi fetchall_arrayref
by Random_Walk (Prior) on May 07, 2013 at 12:37 UTC

    In one case you stringified it. In the other you are taking it in scalar context and Perl magic is not doing what you expect.

    use strict; use warnings; use 5.010; my @array = qw (there is some data here); is_empty (\@array); # look out for edge cases @array = qw(one); is_empty(\@array); #empty it @array = (); is_empty (\@array); sub is_empty { my $array_ref = shift; say "\nGot: ". join ", ", @{$array_ref}; say "Empty" unless scalar @{$array_ref}; say "Caution" if $#{$array_ref} == 0; say "Empty" if $#{$array_ref} == -1; say "Maths == String?" if @{$array_ref} == ""; say "Empty as String?" if @{$array_ref} eq ""; }


    Pereant, qui ante nos nostra dixerunt!
Re: no rows returned for dbi fetchall_arrayref
by ruzam (Curate) on May 08, 2013 at 00:08 UTC

    fetchall_arrayref returns zero if the query failed

    my $result_array = $sth->fetchall_arrayref; if (!$result_array) { print "query failed!!\n"; }

    If no rows are found (the query is successful, but no rows are returned) fetchall_arrayref returns a reference to an empty array.

    my $result_array = $sth->fetchall_arrayref; if ($result_array) { print "success\n"; if (@$result_array) { print "found rows @$result_array\n"; } else { print "no rows returned\n"; } } else { print "query failed!!\n"; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-11-21 17:03 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (244 votes). Check out past polls.