I'm having trouble with fetchall_arrayref() giving a very nonsensical (to me) error. Here's a little test that demonstrates it. (I know fetching results 3 rows at a time is silly, but it's only a test.)
use DBI; my $dbh = DBI->connect('dbi:mysql:test', 'test', 'test') or die; $dbh->do($_) for (split /\s*;\s*/, <<'END_OF_SQL'); drop table if exists foo; create table foo ( bar int ); insert into foo set bar=5; insert into foo set bar=4; insert into foo set bar=7; insert into foo set bar=1; insert into foo set bar=3 END_OF_SQL ##### my $sth = $dbh->prepare("select bar from foo") or die; $sth->execute or die; # $sth->fetchall_arrayref([0], 3) while (my $rows = $sth->fetchall_arrayref(undef, 3)) { for (@$rows) { print "got @$_\n"; } print "---\n"; } __OUTPUT__ got 5 got 4 got 7 --- got 1 got 3 ---
OK. This code runs fine (for me)! But if I change undef to [0] in the fetchall_arrayref() line, I get this output:
__OUTPUT__ DBD::mysql::st fetchall_arrayref failed: fetch() without execute() at +foo.pl line 23. --- DBD::mysql::st fetchall_arrayref failed: fetch() without execute() at +foo.pl line 23. --- [... ad infinitum ..]
What gives? The execute() is fine, and certainly is before the fetchall_arrayref() call. But some people even seem to get these errors with the undef in there! FWIW, I'm using DBI 1.38.

Since my query only returns one column, I should be able to use [0] as the first argument to fetchall_arrayref() to explicitly just fetch the one column. Right?

It's not a huge problem for me to fetch rows one at a time and avoid fetchall_*, but it would be kinda nice to use fetchall_*, as it seems to be the fastest way to get lots of a data.

Additional: It's not the infinite loop that bothers me so much -- that can be worked around. It's that with the [0] argument, the fetchall_arrayref() doesn't work at all and I can't get the query's results.

blokhead


In reply to DBI::fetchall_arrayref() error by blokhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.