fetchrow_hashref()
$sth->fetchrow_hashref([name] )
This function returns a reference to a place in memory
containing a hash of keys and values for one row from the results of a
statement handle. The optional argument of this method is to give the
statement handle name attribute:
NAME (the default), NAME_lc, or
NAME_uc. See the end of this chapter for a
description of these attributes. The name
must be given within quotes if given as a string. Null values
retrieved by this method are returned as undefined. An empty value is
returned when there is an error or when there are no more rows
remaining in the results set. Therefore, if used in a flow control
statement such as while, the empty value returned
will end the loop statement. Here is an example:
...
my $sql_stmnt = "SELECT title, author FROM books";
my $sth = $dbh->prepare($sql_stmnt);
$sth->execute();
while (my $book_ref = $sth->fetchrow_hashref('NAME_uc')) {
print "$book_ref->{'TITLE'} by $book_ref->{'AUTHOR'} \n";
}
$sth->finish();Notice that the name given here with
this method instructs the hash to use all uppercase letters for the
key names. Therefore, when calling the particular data, the column
names are given in all uppercase letters. If no parameter was given,
we could use the column names as they are in the table. The
fetchrow_hashref() method is much simpler
than fetchall_hashref(), but you can’t close
the statement handle until you’re finished processing the results.
Therefore, you may want to consider using
fetchall_hashref() instead.