Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

The fetchall_answer!

by growlf (Pilgrim)
on Oct 20, 2001 at 11:39 UTC ( #120220=note: print w/replies, xml ) Need Help??


in reply to Re: By ref or by ..well..ref!
in thread By ref or by ..well..ref!

Thanks again, all! I have found the way to make fetchall_arrayref() act as expected - good ol' O'Reilly saves the day. To make fetchall_arrayref() pass a reference to an array of references to hashes (instead of a reference to an array of hashes - very subtle diference, but error prone in this need): merely pass the method a template hash to use internally as an example. i.e.:

my $arrayref = $sth->fetchall_arrayref({ FieldID =>1, Fieldname2 =>1, Fieldname3 =>1, Fieldname4 =>1, }); $template->param(table1 => $arrayref);


Update:Or even more simply:
my $arrayref = $sth->fetchall_arrayref({}); $template->param(table1 => $arrayref);


This worked exactly as expected and dropped both code size and execution time dramaticly. Not to mention leaving me with HTML::Template's standard HTML file style that my clients can mess with easily without having to learn anything new. No sense re-writing the wheel, when it does what it is supposed to - once i learned HOW it does it. Took a bit of reading the source of the DBI and HTML libs though. Guess I need to really brush up on passing of references for stuff like this.

Replies are listed 'Best First'.
Re (tilly) 3: By ref or by ..well..ref!
by tilly (Archbishop) on Oct 20, 2001 at 17:27 UTC
    This is the kind of problem which Data::Dumper is very helpful for. If you run it on the database output you will find that you are getting back array references rather than hash references.

    This is the thing I most dislike about DBI's API. There are a ton of efficient ways to use positional logic, but if you want to use name-based logic, you are definitely a second-class citizen. However name-based logic is much better from a development perspective if performance is not utterly critical.

    But anyways you can just use a bit of code like this:

    # Takes a statement handle, and returns the entire result # set as a reference to an array of hashes. sub fetchall_arrayref_hash { my $sth = shift; my @rows; while (defined(my $row = $sth->fetchrow_hashref())) { push @rows, $row; } return \@rows; } # Then elsewhere $template->param(table1 => fetchall_arrayref_hash($sth));
    The fact that someone doesn't provide the interface that you want is no reason not to provide it yourself...
Re: Re: Re: By ref or by ..well..ref!
by uwevoelker (Pilgrim) on Oct 20, 2001 at 14:12 UTC
    Hello,

    fetchall_arrayref() returns a reference to an array of array.
    $ref = $sth->fetchall_arrayref(); $ref = [ ['test1', 1], ['test2', 2] ];
    It does not return a reference to an array of hashes.
    $ref = [ {data => 'test1', ID => 1}, {data => 'test2', ID => 2} ];
    And because of that difference you can not pass it directly to the template-module. But you could change your template-module to accecpt arrays instead of hashes.

    Good bye,
    uwe

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://120220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-08-12 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?