OK. This code runs fine (for me)! But if I change undef to [0] in the fetchall_arrayref() line, I get this output: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 ---
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.__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 ..]
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |