Date sent:              Thu, 01 Feb 2001 15:15:13 -0500
To:                     [EMAIL PROTECTED]
From:                   Kevin Sheen <[EMAIL PROTECTED]>
Subject:                fetchrow_array() help

Kevin, you wrote


> if ($select) {
> 
>         $dbh = DBI->connect( 'xxx', 'xxx', 'xxx' );
>         $sth = $dbh->prepare ( $select )
>                 or die "Can't prepare SQL statement: $DBI::errstr\n";
>         $sth->execute();
>         print "<TABLE border=1>\n";
>         while ( @row = $sth->fetchrow_array() ) {
>                 print "<TR ALIGN=Left>\n";
>                 for ($i=0; $i<$col; $i++) {
>                         print "<TD>$row[i]</TD>";
>                         }
>                 print "</TR>\n";
>                 }
>         print "</TABLE>\n";
>         $dbh->disconnect();
>         }
> 
> 
> The above code is generating the following:
> 
> acer, ken     acer, ken       acer, ken
> allen, william        allen, william  allen, william
> anonymous     anonymous       anonymous
> atkinson, jennifer    atkinson, jennifer      atkinson, jennifer
> auer customer auer customer   auer customer
> 
> It would appear that printing out $row[i] isn't working or I'm doing it wrong 
>(probably, the latter).  Does anyone have any suggestions?

The errors in this code have been already found by others, so let me 
suggest considering to write the inner loop this way:

for (@row) {
        print "<TD>$_</TD>";
}

or, if you don't like $_ :

foreach $item (@row) {
        print "<TD>$item</TD>";
}

I know that this is a style issue and I don't want to sparkle a 
discussion about that, but I believe that your post is a good example 
to demonstrate why Perl-style loops are much less error-prone than 
explicitly working with indices.

Bodo
[EMAIL PROTECTED]
Dr. med. Bodo Eing
Institut fuer Medizinische Mikrobiologie
Klinische Virologie
v.-Stauffenbergstr. 36
48151 Muenster
Germany

Phone: ++49 251 7793 111 Fax: ++49 251 7793-104

Reply via email to