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:
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.select name, address, phonehome, phonework from addressbook;
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:
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:select name from addressbook;
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...$info = { name => "fred", };
You can achieve what you want with the following:
However that's not really the best way to be doing this.my @case; while( my $info = $sth->fetchrow_hashref) { push @case, $info->{name}; } print "@case\n"; # prints built up @case.
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:
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.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.
I hope this helps
jarich
In reply to Re: DBI Fetchrow_hasref issue
by jarich
in thread DBI fetchrow_hashref issue
by jorvic
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |