* [EMAIL PROTECTED] [2003-12-16T07:21:19] > I am using activestate version 5.8 on XP with DBI 1.38. I am trying to > return values into an associative array so that I don't keep needing to > connect to the database. I am new to perl and DBI is looking like rocket > science. The code below has been gleaned from the Internet so I am not > completely sure what is going on. > [...] > while (my $opi = $sth->fetchrow_hashref){ > my %queue = $opi > };
I believe this is your problem, not the other bits. $sth->fetchrow_hashref will return a reference to a hash. So, now $opi is { col1 => val1, col2 => val2 } and so on. You're assigning that to %queue, which is a hash that should have pairs put into it. Do you have strict and warnings on? ("use strict" and "use warnings" at the top of your script.) If so, you should see a message about assinging an odd number of elements to a hash. Even if you said: %queue = ( 1 => $opi ) you'd be overwriting the element in your hash every time. I suggest you do one of two things: (a) use $dbh->selectall_hashref, which will return a hashref (keyed off the column you indicate) of all the rows returned by the select. So, you could say $queue = $dbh->selectall_hashref($sth); print $queue->{$key}{$column}; (b) make %queue an array; if it's really a queue (and you're going to be shifting work off the bottom) an array is more natural. Then the loop would read: while ($sth->fetchrow_hashref) { push @queue, $_; } (I believe this look should work now. I think there's a caveat in the DBI docs that someday fetchrow_hashref may return the same hashref with new values every time.) > IMPORTANT NOTICE This email (including any attachments) is meant only > for the intended recipient. It would be nice if you could not send this disclaimer. I realize that might not be an options. -- rjbs
pgp00000.pgp
Description: PGP signature