in reply to Re^4: DBI hashref does not return data in order of query
in thread DBI hashref does not return data in order of query

I think one of the other posters mentioned a way to get the column names (outside of using the fetchrow_hashref) but, if the query is static you can just create an array of column names and create your own hash, since the fetchrow_arrayref returns data in the order of the SQL select column order. Maybe,
my @cols = ('field1','field2','field3'); while ( my $row = $sth->fetchrow_arrayref ) { # create a hash ref to assign values to cols my $tmp = {}; @$tmp{@cols} = @$row; push @xml_array, $tmp; } # Access fields like so... print $xml_array[0]->{field1},"\n";
I've used this quite a bit in code where I'm reading in files which don't have the column names in them as a header. This should get you a hash per record which sounds like what you want.

Replies are listed 'Best First'.
Re^6: DBI hashref does not return data in order of query
by hallikpapa (Scribe) on Oct 23, 2007 at 17:18 UTC
    It was a misunderstanding on my part. Here is my whole function, and I get an error about coercing an array into a hash.
    my @names = @{ $sth->{NAME} }; my @ordered_values = (); my %xml_hash = ( dataset => [] ); while ( my $row = $sth->fetchrow_arrayref ) { @ordered_values = @{$row}{@names}; <----Can't coerce array in +to hash HERE } my $cgi = CGI->new; print $cgi->header('text/xml'); my $testxml = XMLout( \@ordered_values, NoAttr => 1, RootName => ' +dataset', ); print $testxml;