Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: DBI returning mysql columns in strange order.

by cchampion (Curate)
on Oct 09, 2003 at 22:53 UTC ( #298139=note: print w/ replies, xml ) Need Help??


in reply to Re: DBI returning mysql columns in strange order.
in thread DBI returning mysql columns in strange order.

Wrong.

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"; } }


Comment on Re: Re: DBI returning mysql columns in strange order.
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2015-07-06 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls