my $parent_id = shift; # first retrieve all the results my $results_ref = $dbh->selectcol_arrayref( $prepared_handle, undef, $parent_id); # now deal with the results foreach (@$results_ref) { $results .= $_."\n"; get_children($_); }