Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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?


Thanks,
Spidy

Comment on DBI: Build array of hashes with fetched results?
Download Code
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.

    -Paul

      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.
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://600644]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-20 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (157 votes), past polls