List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:August 18 1999 3:26am
Subject:Re: $sth->fetchrow_hashref
View as plain text  
At 11:01 PM -0400 8/17/99, toxalot@stripped wrote:
>I am working with perl and DBI
>
>I want to print fieldnames with their values and this is what I have.
>
>while ( my $rowref = $sth->fetchrow_hashref ) {
>	foreach $db_field (keys %{$rowref} ) {
>  		print 
>"<TR>\n<TD>$db_field:</TD><TD>$rowref->{$db_field}</TD>\n</TR>\n";
>	}
>}
>
>
>The problem I have is the order that they print in.  It doesn't seem to
>have any rhyme or reason and it certainly doesn't print the order that the
>fields are in.  Is there a simple way to maintain the order.


Perl hashes don't have any order that you can count on.


>Should I be using $sth->{NAME}?  Does that print in order?


Yes.  You could do something like this:

while ($rowref = $sth->fetchrow_hashref ())
{
     foreach my $db_field (@{$sth->{NAME}})
     {
         print 
"<TR>\n<TD>$db_field:</TD><TD>$rowref->{$db_field}</TD>\n</TR>\n";
     }
}

Though perhaps you want something like this instead:

while ($rowref = $sth->fetchrow_hashref ())
{
     print "<TR>\n";
     foreach my $db_field (@{$sth->{NAME}})
     {
         print
"<TD>$db_field:</TD><TD>$rowref->{$db_field}</TD>\n";
     }
     print "</TR>\n";
}

-- 
Paul DuBois, paul@stripped
Thread
$sth->fetchrow_hashreftoxalot18 Aug
  • Re: $sth->fetchrow_hashrefPaul DuBois18 Aug
  • Re: $sth->fetchrow_hashrefJeff Urban18 Aug
    • Re: $sth->fetchrow_hashrefPaul DuBois18 Aug
    • Re: $sth->fetchrow_hashrefSebastian Hoffmann18 Aug
      • Re: $sth->fetchrow_hashrefLuuk de Boer18 Aug
        • Re: $sth->fetchrow_hashrefUrb LeJeune18 Aug