my $dbh = DBI->connect( "DBI:mysql:$database:$db_server", $user, $password ); my $spdtrap_stmt = <<';'; SELECT id, state, city, discription FROM speedtrap WHERE state = ? ; my $comment_stmt = <<';'; SELECT comment_id, comments, name FROM comments WHERE id = ? ; # You need to make two dbh with many databases. # MySQL allows two queries to share the same dbh. # Create two dbh if need be. # Since we're using replaceable parameters, # We can prepare the comment sth once, and # executed multiple times. my $spdtrap_sth = $dbh->prepare($spdtrap_stmt) or die "Couldn't prepare the query: ".$DBI::errstr; my $comment_sth = $dbh->prepare($comment_stmt) or die "Couldn't prepare the comment query: ".$DBI::errstr; $spdtrap_sth->execute($state) or die "Couldn't execute query: ".$DBI::errstr; print <

State

City

Location

Description

Comments

EOF my @spdtrap_row; while (@spdtrap_row = $spdtrap_sth->fetchrow_array) { my ($id, $state, $city, $description) = @spdtrap_row; print < $state $city $location $description EOF $comment_sth->execute($id) or die "Couldn't execute comment query: ".$DBI::errstr; my @comment_row; while (@comment_row = $comment_sth->fetchrow_array) { my ($comment_id, $comment_text, $commenter_name) = @comment_row; print < EOF } print < EOF } print "
Comment By
$comment_text $commenter_name
"; $comment_sth->finish; $spdtrap_sth->finish; $dbh->disconnect;