Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
There's more than one way to do things
 
PerlMonks  

Re: Building an array of hashes from database

by dbwiz (Curate)
on Sep 26, 2003 at 12:37 UTC ( [id://294485]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Building an array of hashes from database

First, immediate solution: put my %data =() inside your loop and it should work.

The reason is that you are pushing the same reference over and over. That's why you see always the same record. If you put the hash inside the block, instead, you make a fresh one at each loop.

However, there are several better ways. The one I prefer is this:

my $sth = $dbh->prepare($query); $sth->execute(); my $records = $sth->fetchall_arrayref({}); # notice an empty hashref here ^

For more ways of dealing with hashes, see DBI Recipes in our Tutorials.

Replies are listed 'Best First'.
Re: Re: Building an array of hashes from database
by iburrell (Chaplain) on Sep 26, 2003 at 16:16 UTC
    DBI has a convienence function on the dbh that does the prepare, execute, and fetchall in one call. Getting an arrayref of hashrefs requires a special argument.
    my $records = $dbh->selectall_arrayref($sql, { Slice => {} });

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://294485]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.