Here is another way to do itmy $sql = "select * from location"; my $sth = $dbh->prepare( $sql ); $sth->execute(); while (my @record = $sth->fetchrow_array) { print join(", ", @record); print "\n"; } $dbh->disconnect();
#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |