Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to add MySql Records To Array or Hash?

by McA (Priest)
on Sep 09, 2013 at 18:45 UTC ( #1053070=note: print w/ replies, xml ) Need Help??


in reply to How to add MySql Records To Array or Hash?

Hi,

the decision whether to use arrays of hashes depends on the size of the result set and other aspects (e.g. maintainability). Hashes and access by field names makes it IMHO more readable.

I would propose the following structure:

my %hash = ( 12345 => { id => 12345, position => 'QB', salary => 15000, points => 20, }, 54321 => { id => 54321, position => 'QB', salary => 14500, points => 19, }, );

To get this structure from a database via DBI have a look at http://search.cpan.org/dist/DBI/DBI.pm#selectall_hashref.

Regards
McA


Comment on Re: How to add MySql Records To Array or Hash?
Download Code
Re^2: How to add MySql Records To Array or Hash?
by jdlev (Scribe) on Sep 09, 2013 at 20:58 UTC
    That looks a lot like what I'm looking for. It says in the DBI documentation that the $key_fields should be called as an array if you want to call multiple columns. Any idea on how to do that? I haven't been able to find any example :(

    I love it when a program comes together - jdhannibal

      You don't need it IMHO as you have ID as primary key, so just set $key_fields to the field name 'ID'.

      One thing you have to be aware of is the case of the identifier. Look at NAME_uc and NAME_lc in the DBI documentation.

      Regards
      McA

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-26 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls