Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How do i get the column 'NAMES' from fetchrow_hashref ???? (http://www.velocityreviews.com/forums/t885059-how-do-i-get-the-column-names-from-fetchrow_hashref.html)

Tommo 02-08-2004 12:15 AM

How do i get the column 'NAMES' from fetchrow_hashref ????
 
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 .....

Tad McClellan 02-08-2004 12:46 AM

Re: How do i get the column 'NAMES' from fetchrow_hashref ????
 
Tommo <mark.thompson@talk21.com> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Bart Lateur 02-08-2004 01:41 AM

Re: How do i get the column 'NAMES' from fetchrow_hashref ????
 
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.


All times are GMT. The time now is 05:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.