Since you are using MySQL, here is a recipe that should
do what you want:
sub get_recs {
my ($dbh, $query, @params) = @_ ;
my $sth = $dbh->prepare($query);
eval {
if (@params) {
$sth->execute(@params);
}
else {
$sth->execute;
}
};
return if $@;
my $table_names = $sth->{mysql_table};
my $field_names = $sth->{NAME_lc};
my $data = $sth->fetchall_arrayref();
my @records;
for my $fn (@$field_names) {
$fn = shift(@$table_names) . '.' . $fn;
}
for (@$data) {
my %rec;
@rec{@$field_names} = @$_;
push @records, \%rec;
}
return \@records;
}
This function will give back the same output of
fetchall_arrayref({}) (with the hashref option, i.e.
an array made of a hashref for each record) with the
table name added to each field.