#get everything from the table my $statement = "select * from $ref"; my $sth = $dbh->prepare($statement) or die "Couldn't prepare statement +: $dbh->errstr"; $sth->execute or die print "Couldn't execute statement: $dbh->errstr"; while ($ref = $sth->fetchrow_hashref){ # must be the first row # print headers if ($count == 1){ print "\t<tr>\n"; # get column names foreach $col (keys %{$ref}){ # do not list id field if ($col eq "id") { next; } # save column names for later push @cols, $col; $col = ucfirst $col; print "\t\t<th>$col</th>\n"; } print "\t</tr>\n"; } print "\t<tr>\n"; #using column names (key) print hash value foreach $col (@cols){ print "\t\t<td>"; print $ref->{$col}; print "</td>\n"; } $count++; print "\t</tr>\n"; } print "</table>\n";
If I use this code for two mysql tables:
For the first table, our perl code displays the columns as I would expect:
| Example | Points |
|---|
The second table displays unexpectedly:
| Cshift | Condition |
|---|
Yet, if I run a the same query from inside the mysql program, the columns are displayed in the correct order. What is wrong?
Neil Watson
watson-wilson.ca
In reply to DBI returning mysql columns in strange order. by neilwatson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |