Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: fetchrow_hashref, and table.column format

by rnahi (Curate)
on Dec 20, 2005 at 20:53 UTC ( #518184=note: print w/replies, xml ) Need Help??

in reply to fetchrow_hashref, and table.column format

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://518184]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-11-20 10:41 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (286 votes). Check out past polls.