while($ref = $sth1->fetchrow_hashref) { print FHExtract join (", ", values %$ref), "\n"; # Closing the statement handler $sth1->finish; } and while($ref1 = $sth2->fetchrow_hashref) { print FHExtract join (", ", values %$ref1), "\n"; # Closing the statement handler $sth2->finish; }