in reply to perl DBI question about fetchrow_arrayref()

fetchrow_arrayref is documented to reuse the same array, so you are saving multiple references to the same array. The simple solution is
my $ary = $dbh->selectall_arrayref($sth_or_stmt); -or- my $ary = $dbh->selectall_arrayref($sth_or_stmt, undef, @bind_values); for (@$ary) { print "@{$_}\n"; }

Replies are listed 'Best First'.
Re^2: perl DBI question about fetchrow_arrayref()
by lightoverhead (Pilgrim) on Apr 25, 2010 at 19:07 UTC

    Yes ikegami. That's why I asked the question.

    "using the same array", what is this same array?

    By my original understanding, $ref returned by fetchrow_arrayref() is the reference points to the row returned by fetchrow_arrayref(), so when I used "push", it should be no problem.

    After I got the answer from sabari, I realized, that the reference pushed into an array is the reference pointed to the current row that fetchrow_arrayref working on.So all the references is a pushed array pointed to the last record. Did I understand this right?

    I don't undertand why perl DBI has created such a seemly unuseful function.

    Thank you.

      "using the same array", what is this same array?

      The one referenced by the reference returned by a call to fetchrow_array and the one reference by the reference returned by a earlier call to fetchrow_array.

      I don't undertand why perl DBI has created such a seemly unuseful function.

      Efficiency. It can avoid creating new arrays all the time, which in turn, allows it to use bind_col internally.

      And contrary to your claims, it's not unusable.
      If you want just one row at a time, then what's the problem?
      If you want all rows, then why aren't you using selectall?

      I don't undertand why perl DBI has created such a seemly unuseful function.
      It's only unuseful if you misuse it. I often want to process result sets row by row, and I only care about the current row, not what the last row was. If I want all the results at once, then (as shown) you can use one of the selectall_* methods.