in reply to Re: Perl database access
in thread Perl database access

I'm running the code you suggested and got it to work. Now if there is a comment I want to print the comment, if there isn't I want to say that there aren't any comments:
$comment_sth->execute($id) or die "Couldn't execute comment query: ".$ +DBI::errstr; my @comment_row; while (@comment_row = $comment_sth->fetchrow_array) { my ($speedtrap_id,$comment_id,$comment_text,$comment_date,$commenter) += @comment_row; if(@comment_row){ print <<EOF; <tr> <td colspan="2" bgcolor="#E6ECF0">$comment_text</td> </tr> EOF } else { print <<EOF; <tr> <td colspan="2" bgcolor="#E6ECF0">There are no comments from others fo +r this speedtrap.</td> </tr> EOF }

But it always executes the the first condition. I've tried all kinds of ways to get the condition to work but I can't figure it out. Your help is greatly appreciated.

Thanks
Adam

Replies are listed 'Best First'.
Re^3: Perl database access
by ikegami (Patriarch) on Sep 23, 2004 at 18:02 UTC

    The problem is you'll only get into the while if @comment_row is true, so testing if (@comment_row) inside the while is useless.

    $comment_sth->execute($id) or die "Couldn't execute comment query: ".$DBI::errstr; my @comment_row; my $comment_count; while (@comment_row = $comment_sth->fetchrow_array) { my ($speedtrap_id,$comment_id,$comment_text,$comment_date,$commente +r) = @comment_row; $comment_count++; print <<EOF; <tr> <td colspan="2" bgcolor="#E6ECF0">$comment_text</td> </tr> EOF } print <<EOF unless ($comment_count); <tr> <td colspan="2" bgcolor="#E6ECF0">There are no comments from others fo +r this speedtrap.</td> </tr> EOF
      AWESOME! It works, thanks a lot!