in reply to MySQL results inserted into Perl variable
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: MySQL results inserted into Perl variable
by locked_user sundialsvc4 (Abbot) on Jul 09, 2014 at 01:06 UTC | |
by Athanasius (Archbishop) on Jul 09, 2014 at 04:37 UTC | |
by Anonymous Monk on Jul 09, 2014 at 01:29 UTC |