How do i get the column 'NAMES' from fetchrow_hashref ????

Discussion in 'Perl Misc' started by Tommo, Feb 8, 2004.

  1. Tommo

    Tommo Guest

    Hello,
    I am stuck trying to get the column names (Keys) from a MYSQL
    table using the Perl fetchrow_hashref method. I can get the values for
    the keys OK.

    while ($column = $sth2->fetchrow_hashref())
    {
    $test = $column->{Wins};
    print "$test\n"; ## value of the column 'Wins' is printed.
    }

    I need to print/store the actual column name, in my scenario I will
    not know what the column names are going to be nor how many .....
    Tommo, Feb 8, 2004
    #1
    1. Advertising

  2. Tommo <> wrote:

    > I am stuck trying to get the column names (Keys) from a MYSQL
    > table using the Perl fetchrow_hashref method.

    ^^^
    ^^^

    Have you read perlreftut.pod yet?


    > I can get the values for
    > the keys OK.
    >
    > while ($column = $sth2->fetchrow_hashref())
    > {



    Apply "Use Rule 1" from the above std doc:

    foreach my $colname ( keys %hash ) # pretend it is a regular hash

    foreach my $colname ( keys %{ } ) # replace hash _name with a block...

    foreach my $colname ( keys %{ $column } ) # ... that returns a hash ref


    Then print the key and value inside the foreach loop's body:

    print "$colname ==> $column->{$colname}\n";


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 8, 2004
    #2
    1. Advertising

  3. Tommo

    Bart Lateur Guest

    Tommo wrote:

    > I am stuck trying to get the column names (Keys) from a MYSQL
    >table using the Perl fetchrow_hashref method. I can get the values for
    >the keys OK.
    >
    >while ($column = $sth2->fetchrow_hashref())


    Actually, you should try to get the column names out of $sth2, via the
    NAME attribute. It's an array reference. so:

    @names = @{$sth2->{NAME}};

    Best do this once, before the loop -- the column names won't change with
    every record.

    See the DBI docs, section "Statement Handle Attributes", item "NAME". It
    has a few siblings.

    --
    Bart.
    Bart Lateur, Feb 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David M. Weprin
    Replies:
    0
    Views:
    416
    David M. Weprin
    Oct 16, 2003
  2. Showjumper
    Replies:
    1
    Views:
    335
    Peter Bromberg [C# MVP]
    Feb 21, 2004
  3. Pradeep
    Replies:
    2
    Views:
    34,005
    Pradeep
    May 31, 2006
  4. swapnil Daingade

    how to get data grid column names

    swapnil Daingade, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    170
    Saravana
    Aug 6, 2003
  5. samasama
    Replies:
    4
    Views:
    116
    samasama
    Sep 20, 2006
Loading...

Share This Page