use CGI qw(:standard *table); ... print start_table; while (my $db_data = $sth->fetchrow_arrayref()) { print Tr(td($db_data)); # other processing in the loop for each element } print end_table;