our approach
|
new tutorials
|
contact us
MySQL Basics In Pictures
Starting
Administration
Tables
Queries
Security
Web
Open the text editor, then open
random.cgi
.
TIP:
Navigate to the
/usr/lib/cgi-bin
directory.
It should show up in the text editor's window:
Edit
random.cgi
to look like this:
#!/usr/bin/perl -w
use DBI;
use CGI qw(:standard);
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"),
h1("Random Quotation:"),
br("\"$quote\""),br
br(" - $first ");
print "$middle " if ($middle);
print "$last\n", end_html();
}
$sth->finish();
# disconnect from database
$dbh->disconnect;
The edited script varies very little from the original
random.cgi
script.
It has been changed to properly display its output in a Web browser, rather than just the terminal window.
<< BACK
NEXT >>