Name

fetchall_hashref( )

Synopsis

$statement_handle->fetchall_hashref(key_column)

This captures the result of a statement and returns a reference to the data. The result is a complex data structure: a hash of references to a hash of each row of data retrieved. By specifying the column to use as a key for the primary hash, a particular row may be accessed and thereby other columns.

...
my $sql_stmnt = "SELECT book_id, title, authors FROM books";
my $sth = $dbh->prepare($sql_stmnt);
$sth->execute( );
$books = $sth->fetchall_hashref('book_id');
foreach my $book_id (%$books) {
   print "$books->{$book_id}->{title} by
             $books->{$book_id}->{author} \n";
}
$sth->finish( );

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.