#!/usr/bin/perl -w # access_cars.pl # A CGI program to illustrate using MySQL from Perl # Get access to DBI and CGI use DBI; use CGI ":standard"; print header(); print start_html("CGI-Perl MySQL database access"); # Create the connection to the database, cars my $dbh = DBI->connect("DBI:mysql:cs655", "cs655", "abc123"); if (!$dbh) { print "Error connecting to database; $dbh->errstr\n"; } # Get the query and display it my $query = param("query"); print "

The query is: ", $query, "

"; # Build a statement object for a SELECT SQL command my $sth = $dbh->prepare($query); # Execute the statement $sth->execute or die "Error - unable to execute query: $dbh->errstr\n"; # Get a reference to the column names in the returned value and # display the column names as the first table row print "", ""; my $col_names = $sth->{NAME}; foreach $field_name (@$col_names) { print ""; } print ""; # Get the rows of the result and display them in the table while (@result_rows = $sth->fetchrow_array) { print ""; while ($#result_rows >= 0) { $field = shift @result_rows; # Replace the HTML special characters with their entities $field = escapeHTML($field); print ""; } print ""; } print "

Query Results

$field_name
$field
"; $sth->finish; $dbh->disconnect; print end_html();