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

This looks like what I want. I put all of the columns I want in the correct order in @fields. In your example you just have $sth->{NAME}, but how would I push the already sorted array into the hash? Or am I missing something simple. I will continue to test. I tried this, and didn't seem to like it
my @names = @{ $sth->{@fields} }; <------Didn't like this while ( my $row = $sth->fetchrow_arrayref ) { @ordered_values = @{$row}{@names}; }
Can't use an undefined value as an ARRAY reference

Replies are listed 'Best First'.
Re^3: DBI hashref does not return data in order of query
by Tux (Canon) on Oct 23, 2007 at 06:17 UTC

    For portability reasons, I'd not use {NAME} at all, but {NAME_lc} or {NAME_uc}. If you do not want to pull out the column names out of the statement handle, (why not btw?), you can do something like ...

    my $sth; my @columns = qw( col1 col2 col3 col4 col5 col6 ); { local $" = ","; $sth = $dbh->prepare ( "select @columns ftom table where day = '10-08-2007' order by +col3, col4"); } $sth->execute; while (my $row = $sth->fetchrow_hashref) { ... }

    Enjoy, Have FUN! H.Merijn