• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Setting all fields in a hash to name/value from sql...

Hi there,

I am just moving code from one server to another, where my current code uses the following syntax to get all the variables returning from the sql output (saves defining each time)....

                while( $hash =  $sth->fetchrow_hashref() )
                                           foreach( keys %$hash ){
                             $$_ = $hash->{$_}

However now the server uses the folowing syntax, and I am not sure how to convert the statement above to "catch all" the output ......

      while (%hash = $sth->fetchhash)

can someone help me out please!
1 Solution
You're looking for:

while (%hash = $sth->fetchhash) {
  foreach (keys %hash) {
    $$_ = $hash{$_};

The only difference here is that you're working with a hash and not a hash reference as in your first example.

Note that neither this nor your code above will work under strict, because you're creating variable variables on-the-fly as it were.

Hope this helps,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now