Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

no rows returned for dbi fetchall_arrayref

by essej1 (Novice)
on May 07, 2013 at 16: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.

Comment on no rows returned for dbi fetchall_arrayref
Select or Download Code
Replies are listed 'Best First'.
Re: no rows returned for dbi fetchall_arrayref
by toolic (Bishop) on May 07, 2013 at 16: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 (Parson) on May 07, 2013 at 16: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 ""; }

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: no rows returned for dbi fetchall_arrayref
by ruzam (Curate) on May 08, 2013 at 04: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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2015-07-28 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls