[Melbourne-pm] fetchrow_array() vs fetchrow_arrayref() WAS RE: Perl DBI reference recommendations

Leigh Sharpe lsharpe at pacificwireless.com.au
Thu Jul 16 18:47:34 PDT 2009


Hi All,

Sam's example earlier used this:
 
>while (my $row = $sth->fetchrow_arrayref) {
>  my ($foo, $bar) = @$row;
>  print "$foo  $bar\n";
>}

Is there any advantage to using fetchrow_arrayref() here, instead of:

while (my ($foo, $bar) = $sth->fetchrow_array()) {
  print "$foo  $bar\n";
}

The latter just 'feels' more intuitive to me, but am I missing something
here which makes a reference more applicable?

Leigh.

-----Original Message-----
From: melbourne-pm-bounces+lsharpe=pacificwireless.com.au at pm.org
[mailto:melbourne-pm-bounces+lsharpe=pacificwireless.com.au at pm.org] On
Behalf Of Sam Watkins
Sent: Thursday, 16 July 2009 5:39 PM
To: alec.clews at gmail.com; melbourne-pm at pm.org
Subject: Re: [Melbourne-pm] Perl DBI reference recommendations

> I want a handy 'desk' reference with
>
> a) Lots of examples
> b) Easy to read and complete reference material
>
> The reason is that I can't hold the details in my head (because I do 
> not DBI regularly) , but I need to keep knocking off dirty that little

> scripts that run sql queries. I figure the easiest way for that is SQL

> via DBI rather than an abstraction module.

Here is a little "handy desk reference" for you, covering all of DBI
that you should use.  It is shorter than the manpage or a book ;)

my $dbh =
DBI->connect("DBI:mysql:database=$database;host=$hostname;port=$port",
$username, $password);
my $sth = $dbh->prepare("SELECT foo, bar FROM baz WHERE foo = ? AND bar
= ?"); $sth->execute("foovalue", $barvalue); 
while (my $row = $sth->fetchrow_arrayref) {
  my ($foo, $bar) = @$row;
  print "$foo  $bar\n";
}
$sth->finish;


You should rarely need to use anything other than the above from DBI. 
keep it simple!

I highly recommend using the "bind values" stuff with the ?s in the SQL
and corresponding parameters to execute.  DBI will quote and escape
values for you correctly so you don't need to worry about SQL injection
attacks, etc.

DBI has umpteen different fetch methods, your life will be easier if you
just use one of them.

regards,

Sam
_______________________________________________
Melbourne-pm mailing list
Melbourne-pm at pm.org
http://mail.pm.org/mailman/listinfo/melbourne-pm


More information about the Melbourne-pm mailing list