Jonas Ask�s wrote:
> I want to to this:
> $SQL = "SELECT * FROM members";
> my $sth = $dbh->prepare($SQL);
> $sth->execute;
> my $record_hash;
>
> while ($record_hash = $sth->fetchrow_hashref){
> print "$record_hash->{first_name} $record_hash->{last_name}\n";
> }
>
> Then I want to search the "$sth" again, and maybe print some other
> information, like this:
>
> while ($record_hash = $sth->fetchrow_hashref){
> print "$record_hash->{last_name} $record_hash->{company}
> $record_hash->{e-mail}\n";
> }
>
> My question is:
> After I've search $sth the first time using the fetchrow_hashref-method, how
> do I point $sth to the first position in the array again, so I can serach it
> again?
*Provided* memory and result set sizes permit, you can put the whole
result set in memory, (as the DBI method fetchall_arrayref does for
records fetched as refs to arrays,) thus:
in an array (@members):
while ($row = $sth->fetchrow_hashref){
push(@members,$row);
}
or as Curt Russell Crandall suggested, in a hash (%members - keyed on a
unique value, e.g. whatever is PK for table 'members', let's say
'member_id'):
while ($row = $sth->fetchrow_hashref){
$members{ $row->{member_id} } = $row;
}