#! /usr/local/bin/perl # Robustness Lab: Solution # # Begin loading the needed extensions # @extensions = ( 'require CGI; import CGI qw (:html2)', 'require CGI::Carp; import CGI::Carp qw (carpout)', 'require DBI; import DBI', ); foreach $ext (@extensions) { print STDERR "$ext\n"; eval $ext; if ($@) { print "Attempt to \"$ext\" failed, this is serious."; } } # # The file dbtype.pl sets a variable which determines the type of database # you will use. This file will be found in either /tmp or /temp. # use lib qw(/tmp /temp); eval { require 'dbtype.pl'; }; if ($@) { print "Attempt to \"require 'dbtype.pl'\" failed,\n"; print "are you sure you put it in /tmp or /temp?"; } # # Set the path, unbuffer STDOUT, and redirect errors to the browser # $ENV{PATH} = join ":", qw(/usr/bin /bin /sbin /usr/sbin /etc); $| = 1; carpout(\*STDOUT); # # Declare variables # my( $dbname ); my( $dbh, $rv, $sth ); # # Accept the CGI request # my( $q ) = new CGI; # # Hard-code the database name # $dbname = "test_nile"; # # Send the HTTP header # print $q->header; # # Set the and send <BODY> # print start_html("Nile dot Com Table Dump"); # # Print out a nice big header, # print h1("Contents of database $dbname"); # # Attempt to load the driver and connect to the database # eval { $dbh = DBI->connect("DBI:$DB_type:$dbname", $DB_login, $DB_password); }; # # Check if the eval failed (driver failed to load) # if ($@) { die "Could not install driver ($DBI::err): $DBI::errstr\n"; } # # Check if the connection attempt was successful # if ( !defined($dbh) ) { die "Could not connect to db \"$dbname\" ($DBI::err): $DBI::errstr\n"; } # # Prepare the following SQL statement and receive a statement handle # eval { $sth = $dbh->prepare(q{SELECT * FROM title}); }; if ($@) { die "Prepare method failed!$@\n"; } # # Execute the prepared SQL statement # eval { $sth->execute or die "Select *: ", $dbh->errstr, "\n"; }; if ($@) { die "Execute method failed!$@\n"; } # # HTML to make the columns all line up properly # print "\n<pre>\n"; # # Class routine to dump the contents of the rows previously SELECT'ed # DBI::dump_results($sth, 100); print "\n</pre>\n"; # # We're done with this statement, free up the resources # $sth->finish; # # Disconnect from the database, we're done! # $dbh->disconnect; # # Send </BODY></HTML> # print end_html; exit 0;