Name

selectall_hashref( )

Synopsis

$database_handle->selectall_hashref($statement, $key_field, 
                                    \%attributes, @bind_values)

This returns a reference to a hash of references to hashes, one for each row from the results of an SQL statement given. This method combines prepare(), execute( ), and fetchall_hashref( ). An optional second argument can specify any of the attributes allowed for a statement handle. If placeholders are used in the SQL statement, their values must be given as an array for the third argument.

...
my $sql_stmnt = "SELECT rec_id, title, author
                 FROM books";
my $books = $dbh->selectall_hashref($sql_stmnt, 'book_id');
foreach my $book_id (keys %$books) {
   print "$books->{$book_id}{title}
          by $books->{$book_id}{author} \n";
}

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.