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

T

Tommo

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 .....
 
T

Tad McClellan

Tommo said:
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";
 
B

Bart Lateur

Tommo said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top