Yep, I eventually figured out that this was the problem.  I was quite
perplexed for some time since the same code which wasn't working for me, was
working for others.  Also, I was scouring different sources of
documentation, which had different API specs.  Finally, I figured out that
this was all due to the change in API... now that I have the right source of
documentation!

At first I did not understand Doug's advice, but then I figured out that he
was right after all... thanks!

So here's the code that works with DBI v1.20.  (It retrieves multiple rows
of data in one shot and returns them as an array of hashtables, which allows
for easy access to column data).

my $stmt = "SELECT * FROM youthLeader";
my $matrix = readTable($stmt);

sub readTable
{
 my $stmt = shift;
 my $dbh = DBI->connect("DBI:mysql:infomgr") or die "Can't connect: " .
$DBI::errstr;
 my $matrix = $dbh->selectall_arrayref($stmt, { Columns=>{} });
 $dbh->disconnect;
 return $matrix;
}

For anyone who uses HTML::Template, $matrix can then be passed directly to
the <TMPL_LOOP> construct for output.  This is very nifty!

Jani

-----Original Message-----
From: Tim Bunce [mailto:[EMAIL PROTECTED]]
Sent: Friday, December 21, 2001 7:56 AM
To: Wilson, Doug
Cc: 'Janakiram Koka'; [EMAIL PROTECTED]
Subject: Re: getting selectall_hashref() to work



Remember that selectall_hashref changed in 1.20. Anyone who asks questions
about selectall_hashref needs to clarify which version of the DBI they're
using.

Tim.

On Wed, Dec 19, 2001 at 09:38:32AM -0800, Wilson, Doug wrote:
>
> > From: Janakiram Koka [mailto:[EMAIL PROTECTED]]
> >
> > I am trying to get the selectall_hashref() method to work.  I
> > have a table
> ....
> > my $matrix = $dbh->selectall_hashref("SELECT * FROM youthLeader");
> > print "value 2 = " . $matrix->[1]{fname} . "\n";
>
> Check the DBI docs again, you don't really want the selectall_hashref
> method, you appear to want the selectall_arrayref method with
> a 'Slice' or 'Columns' attribute argument.
>
> You are referencing $matrix as an array reference (albeit a reference to
> an array of hash references) which is what selectall_arrayref returns.
> selectall_hashref actually returns a reference to a hash (of hashes).
>
> HTH,
> Douglas Wilson

Reply via email to