Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: DBI Fetchrow_hasref issue

by jarich (Curate)
on Feb 21, 2004 at 18:21 UTC ( [id://330839]=note: print w/replies, xml ) Need Help??


in reply to DBI fetchrow_hashref issue

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://330839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found