On Thu, 6 Jan 2005, sam wun wrote:

> Steve Buehler wrote:
> 
> > At 08:11 PM 1/5/2005, you wrote:
> >
> >> Hi list,
> >>
> >> How can I traverse the recordset moret than once?
> >>
> >> For example, the recordset $sth is returned from executing the sql 
> >> statement.
> >> The following statement is writen with the "for" loop for retrieving 
> >> each record from the recordset.
> >>
> >> $aref = $sth->fetchrow_arrayref
> >>
> >> However when the "for" loop is finished, the pointer of the recordset 
> >> is point to the end of the recordset. How can I revise the point to 
> >> the beginning of the recordset ($sth)?
> >

If you hit the end of the result set, $sth->finish is automatically called for
you, and the result set is freed. If you want to go over it more than one time,
your best bet would be to use fetchall_arrayref(), and the loop over that as in:

my $data = $sth->fetchall_arrayref();

for my $row (@$data) {
      # do stuff with @$row;
}

Yes that will use more memory, but remember the entire data set is already
cahced client side, and that cached data set is free()d as soon as it is copied
into a perl data structure. But if you really don't wan't to have 2 copies
hanging around, you can always re-execute:

$sth->execute(@params);

while (my @array = $sth->fetchrow_array) {
   # do stuff
}

$sth->execute(); #execute again
#do more stuff.


Rudy


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to