use strict; use warnings; use DBI; use HTML::Entities; use HTML::Template; my $dbh = DBI->connect( qw(DBI:vendor:database:host user pass), {RaiseError=>1} ); # change these two vars to suite your needs: my @arg = (42); my $sql = 'select foo,bar from baz where qux = ?'; my $sth = $dbh->prepare($sql); $sth->execute(@arg); my $stmt = $sth->{Statement}; my $field = $sth->{NAME}; my $row = $sth->fetchall_arrayref(); my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param( query => $stmt, fields => [ map { { field => ucfirst lc $_ } } @$field ], rows => [ map {{ cols => [ map { { data => defined $_ ? encode_entities $_ : ' ' } } @$_ ] }} @$row ], ); print $tmpl->output; $dbh->disconnect; __DATA__ Dynamic table template