Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

DBI: Build array of hashes with fetched results?

by Spidy (Chaplain)
on Feb 17, 2007 at 23:08 UTC ( #600644=perlquestion: print w/replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Fellow Monks.

For a project I've been working on lately, we have a MySQL table set out like this:

id link_id descr

And what we're trying to do is retrieve the values of id and descr for any rows that have the same link_id. We want to store all of the data retrieved in an array of hashes upon retrieving it, so that later on we can use a foreach loop on the array to loop through our results and generate the HTML that we need. Does anyone know of a way that I could do this?


Replies are listed 'Best First'.
Re: DBI: Build array of hashes with fetched results?
by jZed (Prior) on Feb 18, 2007 at 00:59 UTC
Re: DBI: Build array of hashes with fetched results?
by punkish (Priest) on Feb 18, 2007 at 06:31 UTC
    my $res = $sth->fetchall_arrayref({});

    will give you a ref to an array of hashes.

    Update: Which you can readily use to populate a <TMPL_LOOP> in samtregar's most excellent HTML::Template. Just make sure to have <TMPL_VAR>'s for all the columns queried, else H::T will croak if used with die_on_bad_params => 1 (a good default setting anyway).


    when small people start casting long shadows, it is time to go to bed
Re: DBI: Build array of hashes with fetched results?
by jettero (Monsignor) on Feb 17, 2007 at 23:15 UTC
    I think you'll have to settle for a selectall_hashref(), which is a hash of hashes. You could force it into an array of hashes, but it would be an expense with no gain. If you just want to foreach through it, choose values( %{ $retval } ) and be happy.


      Nope, the OP doesn't have to settle for selectall_hashref. You can get either an AoA or an AoH with selectall_arrayref. See my reference in my reply to the OP.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://600644]
Approved by jettero
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-01-20 10:23 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.