How do you get DBI (mysql, specifically) fetchall_arrayref({}) to give you both the table AND column name from a SELECT. It always seems to use only the column names, even if you explicit ask for specific "table.column"s. $sth->{NAME*} does not have the originating table for each column either. $dbh->{FetchHashKeyName} might be useful. I must know the truth! See example below. Thanks,
use DBI; use Data::Dumper; my $dbh = DBI->connect( 'DBI:mysql:database=test;host=localhost', 'test', 'test', { RaiseError => 1 } ); sub init { # $dbh->do("DROP TABLE t;"); $dbh->do("CREATE TABLE t (id INT, x INT, name CHAR(2));"); $dbh->do("INSERT INTO t (id, x, name) VALUES (1, 0, 'a'), (2, 1, 'b' +), (3, 1, 'c'), (4, 2, 'd');"); } sub doit { my ($cols) = @_; my $COLS = join(', ', @$cols); my $sql = "SELECT $COLS FROM t AS a, t AS b WHERE a.name = 'b' AND b +.name = 'c' AND a.x = b.x"; my $sth = $dbh->prepare($sql); my $rv = $sth->execute(); my $results = $sth->fetchall_arrayref({}); print Data::Dumper->new([ $sql, $results ], [qw( $sql $results)])->D +ump(), "\n"; } init(); doit(['*']); doit(['a.*', 'b.*']); doit(['a.id', 'a.x', 'a.name', 'b.id', 'b.x', 'b.name']); 1;

In reply to DBI table AND column names in fetchall_arrayref({})? by kstephens

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.