You are only fetching one array into @record at a time, so it should be like this:
my $sql = "select * from location"; my $sth = $dbh->prepare( $sql ); $sth->execute(); while (my @record = $sth->fetchrow_array) { print join(", ", @record); print "\n"; } $dbh->disconnect();
Here is another way to do it
#!/usr/bin/env perl use strict; use warnings; use DBI; use SQL::Abstract; use Data::Dump; my $dbh = DBI->connect("dbi:SQLite:dbfile","",""); my $sql=SQL::Abstract->new; # See perldoc SQL::Abstract my($stmt, @bind) = $sql->select('location'); my $sth = $dbh->prepare($stmt); $sth->execute(@bind); my $data = $sth->fetchall_arrayref({}); dd @$data[0]; # First row from the database as hashref dd @$data[1]; # Second row from the database as hashref dd $data->[0]{field1}; #field1 from first row

Using SQL::Abstract will help avoid SQL injection attacks http://www.stonehenge.com/merlyn/UnixReview/col58.html

You can also grow into DBI::Class and other Object Relational Mappers later on if you need.

In reply to Re: MySQL results inserted into Perl variable by trippledubs
in thread MySQL results inserted into Perl variable by shake7

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.