use DBI; use CGI; use HTML::Template; my $cgi = CGI->new; my $dbh = DBI->connect('dbi:mysql:xxxx:localhost', 'xxxxx', 'xxxx'); my @tmpl_opts = (cache => 1, die_on_bad_params => 0, associate => $cgi); my $sth = $dbh->prepare('select id, name, price, saleprice from products'); my ($id, $name, $price, $saleprice); my $i = 0; my $sum = 0; $sth->execute; $sth->bind_columns(\($id, $name, $price, $saleprice)); while ($sth->fetch) { my $real_price = $price < $saleprice ? $price : $saleprice; $name =~ s/\b(\w)(\w+)\b/\U$1\L$2/g; push @rows, { ID => lc($id), PRICE => $real_price, NAME => $name, ODD => $i++ %2 }; $sum += $real_price; } $sth->finish; my $t = HTML::Template->new(filename => 'db2.tmpl', @tmpl_opts); $t->param(ROWS => \@rows, TOTAL => $sum); print $cgi->header; print $t->output; $dbh->disconnect;