This time we will arrange for Apache to run the script. Let us adapt the previous script to print a formatted list of people matching the name “Anne.” This version is called .../cgi-bin/script_html.
#! /usr/local/bin/perl -wT use strict; use DBI(); my ($ref,$mesg,$dbm,$query,$xname,$sname,$sth,$rows); #print HTTP header print "content-type: text/html\n\n"; # open a database $dbm=DBI->connect("DBI:mysql:database=people;host=localhost",'webserv') or die "didn't connect to people"; # get it back $xname="Anne"; $query=qq(select xname, sname from people where xname like "%$xname%"); $sth=$dbm->prepare($query) or die "failed to prepare $query: $!"; # $! is the Perl variable for the current system error message $sth->execute; $rows=$sth->rows; #print HTML header print qq(<HTML><HEAD><TITLE>People's names</TITLE></HEAD><BODY> <table border=1 width=70%><caption><h3>The $rows People called '$xname'</h3></caption> <tr><align left><th>First name</th><th>Last name</th></tr>); while ($ref=$sth->fetchrow_hashref) { print qq(<tr align = right><td>$ref->{'xname'}</td><td> $ref->{'sname'}</td></tr>); } print "</table></BODY></HTML>"; $sth->finish; # close the database connection $dbm->disconnect;