Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I am trying to figure out why this code is not giving me the result I need.
I want to build an array of hashes from the result of a database query.
Here is my code. At the end of the fetch, it prints only the last record in the table, several times.
my $query = "SELECT name, ip, email from people"; my $sth = $dbh->prepare($query); $sth->execute(); my @records =(); my %data =(); while (my @row = $sth->fetchrow_array()) { $data{name} = $row[0]; $data{ip} = $row[1]; $data{email} = $row[2]; push @records, \%data; } for my $record (@records) { for (qw(name ip email)) { print "$_ => $record->{$_}\n" } print "\n" }
Where is the error?
Thanks for any hint.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Building an array of hashes from database
by dbwiz (Curate) on Sep 26, 2003 at 16:37 UTC | |
by iburrell (Chaplain) on Sep 26, 2003 at 20:16 UTC | |
|
Re: Building an array of hashes from database
by mpeppler (Vicar) on Sep 26, 2003 at 16:39 UTC | |
|
Re: Building an array of hashes from database
by snax (Hermit) on Sep 26, 2003 at 17:03 UTC |