[dancer-users] Iteration of hash in template.

David H untg99 at gmail.com
Thu Oct 1 01:41:33 BST 2015


Correction on my syntax, for reference.

$sth = $dbh->prepare("SELECT foo, bar, baz FROM
table");$sth->execute;$hash_ref = $sth->fetchall_hashref( [ qw
<http://perldoc.perl.org/functions/qw.html>(foo bar) ] );print
<http://perldoc.perl.org/functions/print.html> "For foo 42 and bar 38,
baz is $hash_ref->{42}->{38}->{baz}\n";

 Almost!




On Thu, Oct 1, 2015 at 9:35 AM, Richard Reina <gatorreina at gmail.com> wrote:

>
>
> 2015-09-30 18:00 GMT-05:00 Yitzchak Scott-Thoennes <sthoenna at gmail.com>:
>
>> On Wed, Sep 30, 2015 at 2:41 PM, Richard Reina <gatorreina at gmail.com>
>> wrote:
>> > Well something somewhat strange has happened. When I do:  print
>> > Dumper($pats_ref) . "\n"; from the perl module that handles the DB
>> query it
>> > only prints one row:
>> >
>> > $VAR1 = {
>> >           '8' => {
>> >                    'ID' => '8',
>> >                    'ANAME' => 'KOB',
>> >                    'opp_team' => 'Cardinals',
>> >                    'YEAR' => '2015',
>> >                    'date' => '2015-09-04',
>> >                    'AMOUNT' => '2.00',
>> >                    'IDD' => '3',
>> >                    'CHNAME' => 'DWB',
>> >                    'CHLG_ID' => '12',
>> >                    'SPONSOR' => 'MPK',
>> >                    'season' => 'Fall'
>> >                  }
>> >         };
>> >
>> > This is baffling and very frustrating because when I do the exact same
>> query
>> > from the MySQL console it yields 3 row and not 1 row.
>> >
>> >  ID | CHLG_ID | season | YEAR | date  | opp_team | NAME  | NAME
>> > | AMOUNT | sponsor |
>> >
>> +----+---------+--------+------+------------+----------+-----------+-----------------------------+--------+---------+-------------
>> > |  8 |       9  | Fall   | 2015 | 2015-09-04  | Cardinals    | KOB | AFA
>> > |   2.00 | MPK |
>> > |  8 |      12 | Fall   | 2015 | 2015-09-04 | Cardinals     | KOB | DWB
>> > |   1.00 | RFR  |
>> > |  8 |      12 | Fall   | 2015 | 2015-09-04 | Cardinals     | KOB | DWB
>> > |   2.00 | MPK |
>> >
>> +----+---------+--------+------+------------+----------+-----------+-----------------------------+--------+---------+
>> >
>> >  Any insight as to why this might be happening would be greatly, greatly
>> > appreciated.
>>
>> You are telling DBI to put the results in a hash by ID.  So later rows
>> overwrite earlier ones.
>>
>> You probably want fetchall_arrayref( {} ) instead of fetchall_hashref,
>> but I don't remember seeing what you want to end up doing with the
>> data.
>>
>
> Yitzchak,
>
> I cannot thank you enough for your suggestion. Yes, fetchall_arrayref( {}
> ) worked perfectly. I still do not have a good understanding as to when to
> use fetchall_hashref and when to opt for fecthall_arrayref() instead. I
> really really appreicate your help in getting me straightened out.
>
> Thank you again.
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20151001/dbe5cc46/attachment.html>


More information about the dancer-users mailing list