Databases Reference
In-Depth Information
computer containing the scripts. If the web server is on the same computer as your web
browser, you can use the value localhost for the hostname, so the address to use would
be http://localhost/cgi-bin/HelloWorld.cgi.pl on a Linux system, and http://localhost/
xampp/HelloWorld.cgi.pl on a Windows or Mac OS X system using XAMPP.
If your browser reports that you don't have authorization to access the page, you should
check the permission settings for the file or the directory it's in. It's often helpful to
check the Apache error logfile; we describe how to find this file in “The Apache Error
Log” in Chapter 2. Open the error logfile in a text editor and look near the bottom; you
might find a line such as this one:
[Thu Jun 29 02:26:35 2006] [error] [client 127.0.0.1]
Options ExecCGI is off in this directory:
/var/www/cgi-bin/mysql.cgi.animals.popup.pl
This means that Apache has not been configured to allow CGI scripts to be run in this
directory. The solution is to put the scripts in a directory where CGI scripts are allowed
or to configure your server to allow CGI scripts to run in the directory you want. To
do the latter, you need to create a new entry in the Apache configuration file; we describe
how to locate this file in “The Apache Configuration File” in Chapter 2. For example,
to allow CGI scripts to be executed in the directory /var/www/cgi-bin , you would write:
<Directory "/var/www/cgi-bin">
AllowOverride All
Options ExecCGI
Order allow,deny
Allow from all
AddHandler cgi-script.pl
</Directory>
Back to our script. This first web page was not dynamic; for a slightly more complex
example, let's write a CGI script that connects to the MySQL database and lists the
animals in our pet database, as shown in Example 18-2.
Example 18-2. A CGI Perl script that lists animals from the MySQL database
#!/usr/bin/perl
use strict;
# Connect to the MySQL server, run the query, store the result
use DBI;
my $dbh=DBI->connect("DBI:mysql:host=localhost;database=AnimalDB",
" the_username ",
" the_password ",
{PrintError=>0, RaiseError=>1});
my $results = $dbh->selectall_hashref('SELECT * FROM Animals', 'Name');
$dbh->disconnect();
my $result=
"Content-Type: text/html; charset=UTF-8\n\n".
"<html>".
"<head>".
 
Search WWH ::




Custom Search