Hello all,

I need the column name to be associated with the column value and I need the data order to stay in the same order as they appear in the table.

I am trying to find a way to pull a single row from the database keeping data in the order of the table columns.

I would like to be able to use a join also and keep the data ordered by the tables I specify and then the columns in each table.

I went after the easier problem first. Wanting data associated to a column name meant a hash, but hashes aren't ordered. I thought I could use Tie::IxHash and my problems would all go away. Example below. I've used super search, been through the DBI docs, but been unsuccesfu and definitely need some monk wisdom.
use strict; use Data::Dumper; use DBI qw(:sql_types); use Tie::IxHash; tie my %results, "Tie::IxHash"; ... connect to db .... my $stmt = qq~SELECT * FROM registrants WHERE registrant_id = ?~; %results = % { $dbh->selectrow_hashref($stmt, undef, $registrant_id) } +;
Using the tie gave me the exact same results as not using it, which lead me to believe that the order dbi stores the hash might be out of my control. An example of the more complicated statement, I'd like to use:
my $stmt = qq~SELECT * FROM registrants, hotels, cadavers WHERE re +gistrants.registrant_id = ? AND registrants.registrant_id = hotels.re +gistrant_id~; %results = % { $dbh->selectrow_hashref($stmt, undef, $registrant_i +d) };
Has anybody run across this before?

"Recognizing who we aren't is only the first step toward knowing who we are." - Os Guinness

In reply to Maintaining column name and column order using DBI by Tatnall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.