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

I have a Perl function which uses:

my $data = $dbHandle->fetchall_arrayref();

After a lot of Googling I am led to believe that it can return a null value, but have yet to encounter that, whether my record is found or not. So am just trying to find out for sure.

Replies are listed 'Best First'.
Re: Does fetchall_arrayref() ever return a NULL?
by GrandFather (Saint) on May 06, 2022 at 02:29 UTC

    From DBI's fetchall_arrayref section:

    If called on an inactive statement handle, fetchall_arrayref returns undef.

    There may be other stuff in the DBI documentation that may be of interest. Have you read it?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      My attempt to reproduce this has failed.

      use strict; use warnings; use Test::More tests => 2; use DBI; use Data::Dumper; my $dbh = DBI->connect ('dbi:SQLite:dbname=:memory:', '', ''); my $sth = $dbh->prepare ('SELECT 1 = 1;'); $sth->execute; $sth->finish; ok ! $sth->{Active}, 'Handle is inactive'; my $res = $sth->fetchall_arrayref (); is $res, undef, 'Result is undef' or diag Dumper ($res); diag "DBI $DBI::VERSION";
      $ perl inactive.t 1..2 ok 1 - Handle is inactive not ok 2 - Result is undef # Failed test 'Result is undef' # at /tmp/inactive2.t line 16. # got: 'ARRAY(0x2c41050)' # expected: undef # $VAR1 = []; # DBI 1.643 # Looks like you failed 1 test of 2.

      On this apparently inactive handle it still returns an empty arrayref. Where am I going wrong?


      🦛

        I tried your test using MySQL and it gave the same result. But I checked the handle for errors and it produced "fetch() without execute()". So perhaps returning an empty arrayref is correct, as there was an error. Interesting that SQLite did not throw that error.

        You can't do any fetch after calling finish(), I don't think

        Thank for trying all that.

        In the end, I decided it would just be easier if I checked for null in both places and move on.

        checking for undef in the return, and no records when I check the array size

Re: Does fetchall_arrayref() ever return a NULL?
by kcott (Archbishop) on May 06, 2022 at 05:32 UTC

    G'day bartender1382,

    "After a lot of Googling I am led to believe that it can return a null value ..."

    If you show the results of such "Googling", we can comment on what you found.

    — Ken

Re: Does fetchall_arrayref() ever return a NULL?
by erix (Prior) on May 06, 2022 at 09:05 UTC

    What DBMS?

    What SQL statement?

    Where is the code example?

    What expected output?