Code View: 1 #!c:/ActivePerl/bin/perl.exe
2 use DBI;
3 use CGI qw(:standard);
print header, start_html(-title=>"Team Lookup",
-BGCOLOR=>"#66ff33"
);
4 print start_form,"<font face='arial' size='+1'>
Look up what team? ",textfield('name'),p;
5 print submit, end_form, hr;
6 if(param()) {
7 $team = param('name');
8 $dbh =
DBI->connect("DBI:mysql:host=localhost;database=sample_db;
user=root;password=quigley1") or
print "Connection failed: ". $DBI::errstr;
9 $sth=$dbh->prepare("SELECT name, wins, losses
FROM teams where name = ?"
);
$sth->execute($team);
10 if ($sth->rows == 0){
print "Your team isn't in the table.<br>";
exit;
}
print h2("Data for \u$team");
while(($name,$wins,$losses) = $sth->fetchrow_array()){
11 print <<EOF;
<table border="1" bgcolor="yellow">
<tr>
<th>Name</th>
<th>Wins</th>
<th>Losses</th>
</tr>
<tr>
<td>$name</td>
<td>$wins</td>
<td>$losses</td>
</tr>
</table>
12 EOF
print end_html();
$sth->finish();
$dbh->disconnect();
} # End while loop
} # End if block starting on line 6
The shbang (pound sign, bang line) tells the Web server where Perl is installed so that it can start up the Perl interpreter. This line is necessary for any operating system using the Apache server. We are going to be using the DBI module in this program to talk to the MySQL database. We will use the function-oriented version of the CGI module to use the standard CGI functions to talk to the Web browser. This function starts an HTML form. This form is simple. It will consist of one HTML text field, where the user can enter data. The CGI submit function creates the form's Submit button. Once the user clicks the Submit button, the form will be submitted, and the data that was entered into the text field will be sent to the CGI Perl script for processing. Since the CGI module assigns this Perl script to the ACTION attribute in the HTML form, this Perl script will receive the form data and process it. The CGI module will check to see if there are any parameters, meaning, has the form returned any data? If the params function returns true, that means the form has been submitted and this part of the program will continue. If the params function returns false, then the form has not yet been submitted. The name of the text field in the HTML form was "name". The CGI params function takes the name of the text field as an argument and sends back the value that was assigned to it; in this case, the name of a team. We connect to the MySQL database to use the sample_db database. A SQL statement is prepared with a placeholder that will later be assigned the name of a team from the teams table. The query is executed and sent the name of the team the user selected when he filled out the form. After the result-set is returned from the database, a here doc is started. An HTML table is created within the here doc to present the result set, returned from the database, in a nice structured format on the browser. CGI will send this table to the Web server (Apache) and then on to the broswer (Firefox). This user-defined terminator marks the end of the here doc. Remember that this marker must be in the leftmost column of your Perl script and cannot have any trailing spaces; in fact, it must be terminated with a newline.
|