use DBI; use Template; # a little extra shown for cliarity - first get user type $sql = "SELECT user_type FROM usersTable WHERE (username = $user_name); $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute!" . $dbh->errstr); $user_type = $sth->fetchrow_array(); $sth->finish; # get the attributes of the item ids being requested, in this example, corresponding to all items with attribute x = y $sql = "SELECT item_id, att_2, att_3....att_n FROM theTable WHERE (att_x = 'y'); $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh->errstr); $sth->execute() or die("Could not execute!" . $dbh->errstr); # build a data structure to store them while ($item_id, $att_2, $att_3....$att_n) = $sth->fetchrow_array()) { $data{$item_id} = { att_2 => $att_2, att_3 => $att_3, ..... att_n => $att_n } } $sth->finish; #user TT to output the results $template_file = "my_template.htm"; # this passes the whole data hash to the template $vars = { user_type => $user_type, data => \%data } $template->process($template_file, $vars) || die "Template process failed: ", $template->error(), "\n";