Name

selectrow_hashref( )

Synopsis

$database_handle->selectrow_hashref($sql_statement[, 
                                    \%attributes, @values])

This returns a reference to a hash of one row from the results of an SQL statement given. This method combines prepare( ), execute(), and fetchrow_hashref( ). Attributes that may be given for a statement handle may be provided in a hash for the second argument of this method. If placeholders are used in the SQL statement, their values may be given as an array for the third argument.

...
my $sql_stmnt = "SELECT title, author
                 FROM books WHERE book_id = ?";
my $book_ref = $dbh->selectrow_hashref($sql_stmnt,
                                       undef, '1234');
print "$book_ref->{title} by $book_ref->{author} \n";

Notice that the method captures the names of the columns as the keys to the values in the hash generated.

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.