This is untested except for syntax errors, but should work in theory - it contains snippets of code from working projects.

use DBI; my ($db, $user, $pass, $dbh, $query, $sth, $record, %clients); $db = 'database'; $user = 'user'; $pass = 'password'; ### Log into database die "Couldn't connect to database: " . DBI->errstr if !($dbh = DBI->connect("DBI:mysql:$db", $user, $pass)); ### Select client list $query = 'SELECT * FROM clients'; $sth = $dbh->prepare($query); $sth->execute(); ### Store by record IDN or other identifying field $clients{$record->{'idn'}} = $record while $record = $sth->fetchrow_hashref(); ### Add data from different table $query = 'SELECT * FROM client_data'; $sth = $dbh->prepare($query); $sth->execute(); ### Index on same IDN while ($record = $sth->fetchrow_hashref()) { push @{$clients{$record->{'client'}}{$_}}, $record for qw/client data fields here/; } ### Sort on some field to return to desired order for $record (sort { $a->{'name'} cmp $b->{'name'} } values %clients) { ### Do something with record }

What is the multidimensional data being used for, exactly? There may be a better way to approach this depending on what you're really trying to do.


In reply to Re: Help with MySQL SELECT into multidimensional array by TJPride
in thread Help with MySQL SELECT into multidimensional array by btongeorge

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.