in reply to Re: MySQL hashes with same key
in thread MySQL hashes with same key
He wasn't fetching using fetchrow_arrayref or fetchrow_hashref he was fetching with selectall_hashref and it does make a difference:
SQL> create table mje(mac varchar(10), comment varchar(10)); SQLRowCount returns -1 SQL> insert into mje values('1226', 'comment1'); SQLRowCount returns 1 SQL> insert into mje values('1226', 'comment2'); SQLRowCount returns 1 SQL> insert into mje values('1226', 'comment3'); SQLRowCount returns 1 SQL> select * from mje; +-----------+-----------+ | mac | comment | +-----------+-----------+ | 1226 | comment1 | | 1226 | comment2 | | 1226 | comment3 | +-----------+-----------+ SQLRowCount returns -1 3 rows fetched SQL>
perl -le 'use DBI; my $h = DBI->connect;nect; my $r = $h->selectall_ha +shref(q/select * from mje/, "mac"); use Data::Dumper; print Dumper($r +);' $VAR1 = { '1226' => { 'comment' => 'comment3', 'mac' => '1226' } };
perl -le 'use DBI; my $h = DBI->connect;nect; my $r = $h->selectall_ar +rayref(q/select * from mje/); use Data::Dumper; print Dumper($r);' $VAR1 = [ [ '1226', 'comment1' ], [ '1226', 'comment2' ], [ '1226', 'comment3' ] ];
If you use selectall_hashref or fetchall_hashref you need to provide a key column and if you have multiple rows where that key column has the same value you will lose some rows.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: MySQL hashes with same key
by NetWallah (Canon) on Nov 16, 2012 at 16:19 UTC | |
by jcrush (Acolyte) on Nov 20, 2012 at 15:24 UTC | |
|
Re^3: MySQL hashes with same key
by Anonymous Monk on Nov 16, 2012 at 12:49 UTC |