open (writefile,">>$target_full_path") || die "Can't open $target_full_path"; while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; print writefile $fetchdata; $rowcount++; #here it is counting } close writefile; print $rowcount;