vol7ron
vol7ron

Reputation: 42109

Perl: How to retrieve field names when doing $dbh->selectall_..?

$sth = $dbh->prepare($sql);
$sth->execute();
$sth->{NAME};

But how do you do that when:

$hr  = $dbh->selectall_hashref($sql,'pk_id');

There's no $sth, so how do you get the $sth->{NAME}? $dbh->{NAME} doesn't exist.

Upvotes: 1

Views: 340

Answers (2)

runrig
runrig

Reputation: 6524

You can always prepare and execute the handle yourself, get the column names from it, and then pass the handle instead of the sql to selectall_hashref (e.g. if you want the column names but the statement may return no rows). Though you may as well call fetchall_hashref on the statement handle.

Upvotes: 0

hobbs
hobbs

Reputation: 239990

When you're looking at a row, you can always use keys %$row to find out what columns it contains. They'll be exactly the same thing as NAME (unless you change FetchHashKeyName to NAME_lc or NAME_uc).

Upvotes: 5

Related Questions