Copy the following code and paste it into random.pl.
#!/usr/bin/perl -w
use DBI;
use strict;
# database information
my $db="us_presidents";
my $host="localhost";
my $port="3306";
my $userid="marty";
my $passwd="watch4keys";
my $connectionInfo="DBI:mysql:database=$db;$host:$port";
# find a random number between 1 and 20
my $random=int(rand 20) + 1;
# make connection to database
my $dbh = DBI->
connect($connectionInfo,$userid,$passwd);
# prepare and execute query
my $query = "SELECT first,middle,last,quote
FROM quote,name
WHERE quote.id=$random
AND quote.name_id=name.id;";
my $sth = $dbh->prepare($query);
$sth->execute();
# assign fields to variables
my ($first,$middle,$last,$quote);
$sth->bind_columns(undef, \$first, \$middle, \$last, \$quote);
# output random quote
while($sth->fetch()) {
print header(), start_html("Random Quotation");
print " Random Quotation: $quote
- $first ";
print "$middle" if ($middle);
print "$last\n
", end_html();
}
$sth->finish();
# disconnect from database
$dbh->disconnect;