They are not the same thing, though.
Not only fetchrow_hashref will be slower, because the names are assigned for each row, while $sth->{NAME} is only evaluated once, after the call to "execute," but in addition, if your query contains two columns with the same name, $sth->{NAME} will handle it, but fetchrow_hashref will not.select * from dept; select * from emp; +--------+----------+ | deptID | name | +--------+----------+ | 1 | pers | | 2 | sales | | 3 | research | +--------+----------+ +-------+-------+--------+ | empID | name | deptID | +-------+-------+--------+ | 1 | John | 1 | | 2 | Fred | 2 | | 3 | Susan | 2 | +-------+-------+--------+ select distinct dept.deptID, emp.deptID from dept left join emp using(deptID); +--------+--------+ | deptID | deptID | +--------+--------+ | 1 | 1 | | 2 | 2 | | 3 | NULL | +--------+--------+
Given these tables, the Perl code to handle them could be:
my $query = qq{select distinct dept.deptID, emp.deptID from dept left join emp using(deptID)}; my $sth = $dbh->prepare($query); $sth->execute(); print "--- names\n"; print join ", ", @{$sth->{NAME}}; print "\n--- fetchrow_hashref\n"; my $row = $sth->fetchrow_hashref(); print "$_ \t" for keys %$row; print "\n"; $sth->finish(); __OUTPUT__ --- names deptID, deptID --- fetchrow_hashref deptID
Notice that $sth->{NAME}, despite the double names, returns the correct result. fetchrow_hashref, instead, misses one column altogether.
In reply to Re: Re: Field names from DBI?
by cchampion
in thread Field names from DBI?
by Massyn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |