#! /usr/local/bin/perl -w # Robustness Lab: Solution # # Begin loading the needed extensions # use CGI qw (:html2); # The CGI extension use CGI::Carp qw (carpout); # The extra carping routine use DBI; # The DBI extension # # 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); require 'dbtype.pl'; # # 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; # # Read values from the form which called us. # Note that the variable from the form is different than from the perl variable. # This is deliberate. # $dbname = $q->param("db_name"); if ( !defined($dbname) || $dbname eq "" ) { $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 # $sth = $dbh->prepare(q{SELECT * FROM title}); # # Execute the prepared SQL statement # $sth->execute or die "Select *: ", $dbh->errstr, "\n"; # # HTML to make the columns all line up properly # print pre; # # Class routine to dump the contents of the rows previously SELECT'ed # DBI::dump_results($sth, 100); # # 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;