Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: DBI returning mysql columns in strange order.

by kutsu (Priest)
on Oct 09, 2003 at 21:32 UTC ( #298112=note: print w/replies, xml ) Need Help??

in reply to DBI returning mysql columns in strange order.

you might change

my $statement = "select * from $ref"; to my $statement = "select * from $ref order by foobar";

which will order it by what ever column foobar is

"Pain is weakness leaving the body, I find myself in pain everyday" -me

Replies are listed 'Best First'.
Re: Re: DBI returning mysql columns in strange order.
by cchampion (Curate) on Oct 09, 2003 at 22:53 UTC


    That won't solve the problem, which is the order of columns returned from a hash, not the order of records in a data set.

    Since column order seems to be important, a good piece of advice would be usine specific column names (as dragonchild said) in the select statement, and fetching records with fetchrow_arrayref.

    If a hash is also needed, then it must be integrated by a list of columns.

    Consider this (untested) example.

    my @columns = (qw(col1 col2 col3)); my $query = "SELECT " . join (",", @columns) . " FROM tablename"; my $sth = $dbh->prepare($query); $sth->execute; while (my $href = $sth->fetchrow_hashref) { for (@columns) { print "$_ => $href->{$_}\n"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://298112]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-06-19 14:57 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.