List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:August 18 1999 3:14pm
Subject:RE: fetchall_arrayref
View as plain text  
At 9:27 AM -0400 8/18/99, Dan Ray wrote:
>Fred--
>
>> I would like to store all rows in a variable and return it
>>
>> I tried
>> $tbl_ary_ref = $sth->fetchall_arrayref({ ID_AD=>1});
>
>Okay, first of all, the fetchall_arrayref method doesn't fetch all rows, it
>fetches all columns from a previously SELECT'ed set of rows, one row at a
>time.

That is incorrect.  fetchall_arrayref() does fetch all rows.  It
returns a reference to an array of references, each one of which is
a reference to the values for one row.   (Basically, it returns a
value that you can treat as a reference to a matrix.)

If you call fetchall_arrayref() with no arguments or an array slice
as an argument, you can access the return value members like this
(this is just an example that prints the values returned):

my ($matrix_ref);  # reference to array of references
my ($rows, $cols); # size of matrix
$matrix_ref = $sth->fetchall_arrayref ();

$rows = (!defined ($matrix_ref) ? 0 : scalar (@{$matrix_ref}));
$cols = ($rows == 0 ? 0 : scalar (@{$matrix_ref->[0]}));

for (my $i = 0; $i < $rows; $i++)
{
my ($delim) = "";
     for (my $j = 0; $j < $cols; $j++)
     {
         print $delim . $matrix_ref->[$i][$j];
         $delim = ",";
     }
     print "\n";
}


If you call fetchall_arrayref() with a hash reference argument, as
is done above, you can access the return value members like this
(this example uses your ID_AD column):

my ($matrix_ref);  # reference to array of references
my ($rows, $cols); # size of matrix
$matrix_ref = $sth->fetchall_arrayref ({ID_AD => 1});

$rows = (!defined ($matrix_ref) ? 0 : scalar (@{$matrix_ref}));
$cols = ($rows == 0 ? 0 : scalar (keys (%{$matrix_ref->[0]})));

for (my $i = 0; $i < $rows; $i++)
{
     print $matrix_ref->[$i]{ID_AD};
     print "\n";
}

-- 
Paul DuBois, paul@stripped
Thread
fetchall_arrayrefFRed18 Aug
  • RE: fetchall_arrayrefDan Ray18 Aug
    • RE: fetchall_arrayrefPaul DuBois18 Aug
      • RE: fetchall_arrayrefDan Ray18 Aug
        • Re: fetchall_arrayrefThimble Smith18 Aug
          • Re: fetchall_arrayrefPaul DuBois18 Aug
            • Re: fetchall_arrayrefThimble Smith18 Aug
              • Re: fetchall_arrayrefPaul DuBois18 Aug
      • Re: fetchall_arrayrefScott Hess18 Aug
  • Re: fetchall_arrayreffelix k sheng18 Aug
RE: fetchall_arrayrefDarren L Sweeney18 Aug
Re: fetchall_arrayrefPaul DuBois18 Aug