I'm kind of a beginner, but here is how I would start. I like to use SQL::Abstract and then use empty braces in the fetchall statement to make each element in the array a hash of that row from the SQL table.

#!/usr/bin/env perl use strict; use warnings; use SQL::Abstract; use DBD::mysqlPP; use Data::Dumper; my $dsn = "dbi:mysqlPP:database=$database;host=$hostname"; my $dbh = DBI->connect($dsn,$user,$pass); my $sql = SQL::Abstract->new; my ($stmt,@bind) = $sql->select('quarterbacks'); my $sth = $dbh->prepare($stmt); $sth->execute(@bind); my $results = $sth->fetchall_arrayref({});

Your results ref is an array of hashes. So your first full record might be

$results->[0]; // Reference to a hash ref of Row_1 my %row_1 = %{$results->[0]}; // Hash of Row 1 $results->[0]{name}; // Name column from Row_1 // to iterate over the record set you might do this: // id, position, name are column names from the table for (@{$results}) { my $id = $_->{id}; my $position = $_->{position}; my $qb_name = $_->{name}; }

In reply to Re: How to add MySql Records To Array or Hash? by trippledubs
in thread How to add MySql Records To Array or Hash? by jdlev

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.