#!/usr/bin/perl -wT use DBI; use CGI qw(-no_debug :standard); use CGI::Carp qw(fatalsToBrowser); my $dbh = DBI->connect("DBI:mysql:hostname=localhost:database=Tkil_CDs", "tkil_ro", "") or die DBI->errstr; my $sth = $dbh->prepare(<errstr; SELECT Artist.name, Album.name, Formats.name, Album.year FROM Artist, Album, Formats WHERE Artist.id = Album.artist_id AND Album.format_id = Formats.id ORDER BY Artist.sort_key, Album.year, Album.name SQL # $sql_string =~ s/\s+$//; $sth->execute() or die $sth->errstr; # original from rkitover # print table( map { map { TR(td($_)) } (@$_) } ($sth->fetchall_arrayref) ); # mine (hairy version): # print table( map { Tr($_) } map { td($_) } @{ $sth->fetchall_arrayref } ); # print p(), "Rows retrieved: ", $sth->rows, p(); # mine (obvious version). # print CGI header and top of my HTML document print < Tkil CDs HTML while (my $row_aref = $sth->fetch) { # get each field my ($artist, $album, $format, $year) = @$row_aref; # make sure it's safe HTML for ($artist, $album, $year) { s/&/&/g; s//>/g; } # workman uses "//" to separate lines. let's try to accommodate # that. for ($artist, $album, $year) { s!//!
  !g; } # we only want to see a format if it's not CD. if (!$format || $format eq 'CD') { $format = ''; } else { $format = " [$format]"; } # and print the row print ' ', '', "", "", "", "\n"; } # and print the closing HTML bits. print <
Tkil CDs
Artist Album Year
$artist$album$format$year
HTML # and disconnect from the database $dbh->disconnect(); # bye! exit(0);