Nik has asked for the wisdom of the Perl Monks concerning the following question:

I was tryign to loop a list of displaying games since in every row i had the submit button, the description and the how many times the user download the file information. Although the following code has no error nothing is being dispalys. why?!
$st = $db->prepare( "SELECT * FROM counter" ); $st->execute(); $row = $st->fetchrow_hashref; @tableRows = (); while( $item = $row ) { push @tableRows, $item; }; print table( {class=>'info'}, map { Tr( td( submit( -name=>'game', -value=>$_->{name} )), td( $_->{text} ), td( $_->{name} ) ) } @tableRows );

Replies are listed 'Best First'.
Re: Looping question
by jZed (Prior) on Apr 22, 2005 at 21:23 UTC
    $row = $st->fetchrow_hashref;
    That only returns a single row. Perhaps you mean to use a loop (while my $row = $st->fetchrow_hashref ...) or perhaps you meant to use $dbh->selectall_hashref or another database method that returns multiple rows.
Re: Looping question
by 5mi11er (Deacon) on Apr 22, 2005 at 21:24 UTC
    You've got yourself an infinite loop right before the print statement. Perhaps you meant  while($item == $row) {??

    -Scott

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Looping question
by shemp (Deacon) on Apr 22, 2005 at 21:42 UTC
    Other people eluded to this, but here's specific code for what you probably want.
    ... my @tableRows; while ( my $row = $st->fetchrow_hashref() ) { push @tableRows, $row; } ...
    2 other things to keep in mind:
  • does your query actully return any rows, or is the counter table empty?
  • Is the info in @tableRows of the format desired by table() - CGI function im guessing?
    A reply falls below the community's threshold of quality. You may see it by logging in.