in reply to Not sure how to query my database in this code

Here's an approach based on the iterator nature of a DBI statement handle:
my $sth = $dbh->prepare("SELECT id, filename, title FROM pictures"); $sth->execute(); print "<table ...>"; for my $row (1..$nrows) { print "<tr>"; for my $col (1..$ncols) { print "<td>"; my $pic = $sth->fetchrow_hash; if ($pic) { # emit html for the image # data for the picture is in hashref $pic, i.e.: # $pic->{id}, $pic->{filename} and $pic->{title} } else { # emit html for an empty cell } print "</td>"; } print "</tr>\n"; } print "</table>\n";
Note that there is no need to compute the number of empty <td> elements to create - the return value of fetchrow_hash (either a hashref or undef) will indicate what should go in the cell.