my $dbh = DBI->connect( qw(DBI:mysql:database:host user pass), #substitute your values here {RaiseError => 1 }, ); my $sections = $dbh->selectall_arrayref(' SELECT id, title FROM sections ',{Slice => {}}); for my $section (@$sections) { my $pages = $dbh->selectall_arrayref(' SELECT SELECT title FROM pages WHERE sectionid = ? ',{Slice => {}}, $section->{id}); $section->{pages} = $pages; } my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(sections => $sections); print $tmpl->output; __DATA__