Another technique for dealing with "relat(ing) a value to it's column name" is to use
$sth->fetchrow_hashref
This returns an anonymous hash for each row, with the column names as keys. I use this a lot (hashes are the best data type for most things in Perl, anyways). Normally it's bad to
SELECT *
There are obvious performance reasons for retreiving only the data you need, but if you're usually needing most of the columns in a row, then the difference is often slight. The real reason not to SELECT * is that when you use $sth->fetchrow_array or any other such methods that retrieve the row as a list, then if you change the table schema, you might get your data back in the row order. With none of your code having been changed! But if you use $sth->fetchrow_hashref (or $sth->fetchall_hashref, etc) then this isn't a problem, because the keys in the hash are the column names. So not only you can safely SELECT *, but then when you do, and your table schema has changed, you don't have to modify your SQL. You can either just ignore the new columns, or if you need them, there they are in your hashes. Poof!

I like this approach, because when I make changes it Does What I Mean.


--
Snazzy tagline here

In reply to Re: Perl-MySQL get column names by Aighearach
in thread Perl-MySQL get column names by kiat

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.