I've had to invent a lot of code as you don't show much of your program, but the program below gives you the rough idea
Once you've called execute you have to call one of the fetch methods to retrieve the data in whatever form is most useful to you. Here I've just asked for a reference to an array containing each row's data
Then it's simply a matter of opening the required file for output and printing the rows of data to it
I've removed the status checks on each DBI call and replaced it with the RaiseError flag which does the same thing automatically. I've also replaced the parameter $mycrnum in the SQL statement with a placeholder and passed its value to execute. That way DBI looks after any necessary quoting etc.
use strict;
use warnings;
use DBI;
my ($dsn, $user, $pass);
my ($mycrnum, $mydir);
my $dbh = DBI->connect($dsn, $user, $pass);
@{$dbh}{qw/ PrintError RaiseError /} = (0, 1);
my $sth = $dbh->prepare(
"SELECT COUNT(parameter2),
parameter2 AS file_type
FROM kcrt_table_entries
WHERE request_id = ?
GROUP BY parameter2"
);
$sth->execute($mycrnum);
open my $fh, '>', "$mydir/file_detail.txt" or die $!;
select $fh;
while ( my $row = $sth->fetchrow_arrayref ) {
printf "%5d %s\n", @$row;
}