It would make the code cleaner and easier (I thought) if I fetched a hash instead of an array (like usual).

SNIP

What I'm trying to end up having happen is my CGI script will have popup menus. I was wanting those popup menu's populated with entries from the SQL query. To make this easier I thought I would fetch a hash, dump all of the values from the hash into an array and use that array to in the popup menu.

fetchrow_hashref is best when you're selecting a number of things out from the database and you don't want to have to care what order you're doing that in. For example:

select name, address, phonehome, phonework from addressbook;
would give us a reference to a hash with the keys "name", "address", "phonehome", "phonework" and their values would be the values from the db. Note that this hash would only contain the values for ONE entry. The next time we called fetchrow_hashref we'd get the values for the next entry. And so on.

Considering that you've said that you just want to dump all the values from the hash into an array I get the distinct impression that you're SQL is more like:

select name from addressbook;
If this is the case then your code isn't going to do what you want it to anyway. $info is going to be set to something like:
$info = { name => "fred", };
and you're going to print out "name". Even if you try to capture all of the values from this hash you're only going to get "fred" this time through the loop and "julie" next time through the loop...

You can achieve what you want with the following:

my @case; while( my $info = $sth->fetchrow_hashref) { push @case, $info->{name}; } print "@case\n"; # prints built up @case.
However that's not really the best way to be doing this.

I think what you're looking for is selectcol_arrayref. This selects all the (appropriate values) from a single column and returns an array ref to them. So this:

my $names = $dbh->selectcol_arrayref( "select name from addressbook where name like ?", undef, $name ) or die "select failed. " . $dbh->errstr; my @case = @$names; # If you'd rather deal with an array print "@case\n"; # prints out ALL of the names selected.
gives you all the values in the "name" column of the database which matched your where clause. You can then use $names (or @case in this instance) to do whatever you need.

I hope this helps

jarich


In reply to Re: DBI Fetchrow_hasref issue by jarich
in thread DBI fetchrow_hashref issue by jorvic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.