$a = find_items("26"); print "$a"; sub find_items { $idd = shift; $sth3 = $dbh->prepare("SELECT category FROM items WHERE category = '$idd'"); $sth3->execute or die $dbh->errstr; $rvf = $sth3->rows; $a += $rvf; $sth23 = $dbh->prepare("SELECT id FROM category WHERE parent = '$idd'"); $sth23->execute or die $dbh->errstr; while ($idg = $sth23->fetchrow_array) { $item = find_items($idg); $a += $item; } return($a); }