Broad Network

Making PurePerl MySQL API work with Apache Web Server

Web Development Basics with Perl and MySQL – Part 2

Using Apache Web Server

Foreword: In this part of the series, I talk about the basic Perl principles for interfacing MySQL Server.

By: Chrysanthus Date Published: 27 Apr 2016


This is part 2 of my series, Web Development Basics with Perl and MySQL. In this part of the series, I talk about the basic Perl principles for interfacing MySQL Server. You should have read the previous parts of the series before reaching here, as this is a continuation.

Information of the web page at the client’s computer meant for the MySQL database has to be received by a script at the server first. It is this server script that will send the information to the database, which is also in the server host computer (everything being equal). Information flow in the reverse direction from the database passes through a script before reaching the client. In this series the scripts at the server are written in Perl. Everything taught in this part of the series can be placed in one server script. In practice, some of the principles would be used in one script, while others would be used in other scripts. You can have more than one Perl server script in an application (web development).

When the server script and the Database server are in the same computer, we say the script and the database have the same host called, localhost. In this series you will use localhost for the code samples. For your commercial project, if the script and the database server are in two different computers, then in the script code you will have to replace "localhost" with the domain name that has the database server.

API stands for Application Programming Interface. This is software that is placed between two other software. The API facilitates communication between the two software. The API you will use in this series is called PurePerl MySQL API. PurePerl stands for Pure Perl. This is Perl software without the C language. This API is far more convenient to install and far more convenient to use, when you compare it with what is out there. Hey, the API is free!

I will explain in this and the next section, what I have done to get my Perl interpreter, the API, a Web Server, and MySQL server to work together. You will do the same or a similar thing to have your own four components, work together. The 4 components are installed in the same hard drive.

I use ActivePerl version 5.18, PurePerl MySQL API version 1.01, Apache Web Server and the Windows Operating System. If you are not using the Windows Operating system, then in the code sample scripts, you will have to type something like, #!/usr/bin/perl as the first line of the script. That gives the path to the Perl interpreter (engine) and the name of the interpreter file.

ActivePerl 5.18 is free. To download it, just search the web. The PurePerl MySQL API is also free. You can download it from the link:

PurePerl MySQL API

Apache Web Server 2.2 is free. To download it, just search the web; you can use a newer version. The MySQL server is free. To download it, just search the web. I use Windows XP, which you can get free or beg from a friend or colleague. You can use the higher version of any of these components; everything said in this series will still work. For the rest of this tutorial, I assume your operating system is already installed and is on.

To install MySQL, I double clicked the downloaded file, mysql-5.5.19-win32.msi. I allowed the default settings. After the MySQL main installation, the MySQL Sever Instant Configuration Wizard Window opens presenting the second installation phase for MySQL. In this second phase, I chose or allowed the following settings in their different windows : Detailed Configuration, Developer machine, Multifunctional Database, Decision Support, Enable TCP/IP Networking, port 3306, Enable Strict Mode, Standard Character set of Latin1, Install as Windows Service-Server, Name of MySQL, Include Bin Directory in Windows Path, Modify Security Settings-Enter, Your Password.

For MySQL password, you will type the password of your choice. Allow your user name as “root”, which is for the database administrator.

To install ActivePerl I double clicked the downloaded file, 1802-MSWin32-x86-64int-298023.msi choosing all the features and allowed default settings. I also allowed the windows environment variable to be set.

To install the PurePerl MySQL API, I unzipped the downloaded file, . I copied all the contents I saw into my computer’s C:\Perl\lib directory, and that was it. The directory opened had the files, and, and another directory, Mysql. The files and plus the directory, Mysql with all its content, were copied to C:\Perl\lib in my personal computer. You should have the same or a similar directory, like C:\Perl\lib in you system; use the same or the similar directory.

To install Apache web server, I did the following:

After searching the web for the free Apache HTTP server 2.2 or 2.4, install the web server in your computer following the instructions you saw in the web site. It should be installed with the domain name, localhost (and not something like,

In order for the web server to execute your scripts, you need to indicate to the web server, the directory (and subdirectories) in which your scripts are. You scripts should be in an Apache sub directory. In my computer, the directory is:

    "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/"

You also have to indicate to the web server the extension of your script files, which is .pl .

Apache web server comes with a configuration file. In my computer the name of the configuration file is, httpd.conf and it is found in the directory,

C:/Program Files/Apache Software Foundation/Apache2.2/conf

To indicate the directory of my Perl script files and the extension for perl files, I typed the following text at the bottom of the configuration file:

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/">
    Options +ExecCGI
    AddHandler cgi-script .pl

The web server should also know the file name of the Perl interpreter and its path. This is indicated to the web server in every script file with a line like,


at the top of each script file

If you are using the windows operating system, then at this point, your installation is complete, and the 4 components can now work together. Now, if you are not using the windows operating system, then go to the file of your computer, in the installed directory, and at the top of the file, type something like, #!/usr/bin/perl pushing down the rest of the content, by one line. Do the same for all the other files in the C:\Perl\lib\ Mysql directory of your computer. At that point your installation is complete and the four components can work together.

Any application in a computer in the Internet (network), has two important figures associated with it. One called the IP address or domain name identifies the computer in the network. The other called the port, identifies the application in the computer. In a computer, the MySQL server is an application. The databases it produces are products and parts of the application. The web server is also an application. The web pages and scripts it works with are components that integrate themselves to the application.

In my system the port for the MySQL server is, 3306; this is the default port. If the Abyss Web Server is the only web server you have installed in your computer, then port 80, which is the default web server port, should be the port for it. The default port 80 for a web server is usually not typed at the address bar of the browser.

Basic Web Development Script Coding
In this section I talk about the basic script coding, using the famous PurePerl MySQL API. This API is easy to install and easy to use as you saw above. When the script at the server receives data from the client it sends it to a database or an email box. In this section I talk about the script working with the database. I will talk about the script working with the email box later.

The code begins with:

    use Mysql;
    use strict;

The first statement brings in the Mysql API for the script to use. The next statement says that the Perl coding in the script, has to be strict. I am using the Windows Operating System, and I had to type the very first line.

Assume that the database you want to use is already created at the server, then your code should follow the following skeleton:

    if (!Mysql::connect("root", "xxxxxx", "localhost", 3306))
            print $Mysql::Error_msg;

            #select the database
            if (!Mysql::select_db("PetStore"))
                    print $Mysql::Error_msg, "\n";

                    #do whatever you want to do with the database here!


So, whether the script is in the sane computer as the database server or not, you have to connect to the database server. To connect you need a username. Above, the user name is “root”, the database administrator. You need a password. Above, the password is xxxxxx; you have to replace this with your own. You need the domain name of the database server. In this series, the server script and the database server are in the same computer. So the domain name is localhost. If they are not in the same server, then you need to type the domain name of the database server, in place of “localhost”. You also need the port of the database server. In this series the default port is used, which comes with the default MySQL installation; you have to type the port.

In the script, you begin any API variable with “$Mysql::” without the quotes. When the API encounters an error it places it in its variable, Error_msg. To access it in your script, you have to type, $Mysql::Error_msg. The API stores other data, which are not error, in other variables. You access them in the same way – see later. Note the casing of the preceding, $Mysql::; it is not $MySQL::

The API has a function called connect(); you begin it and other API functions in your script with Mysql:: , as shown above. Within the else block of the connect command (function), you start by selecting the database you want to work with. The function is select_db(); note how it has been called above.

The connect() function and the select_db() function, each returns 1 for true on success, and 0 for false on failure. You must select a database before you use it. If there is any error from any of the functions, you get the error message from the $Mysql::Error_msg variable.

Within the else block of the select database command, you do whatever you want to do with the database.

Just before the closing curly brace, } of the connect() command, you close the connection. Well, you can actually close the connection before or after this brace, }. Any connection that is opened, must be closed. That is why you have the line:


The close() function is defined in the API. It is a function, so, in your script, you begin it with, “Mysql::”.

If you have the patience, you can type the following instead of just the “Mysql::close();” statement:

            if (!Mysql::close())
                    print $Mysql::Error_msg;
                    print "Connection closed.";

The API query() Function
The API has a function whose syntax four your script, is:

    Boolean Mysql::query(SQLstr)

It returns 1 for success and 0 for failure. You use this function to do must of your work with the database. There is only one argument, and it is a MySQL SQL statement without the ending semicolon. The statement has to be within double quotes, to form a string argument. You should already have a good understanding of SQL statements. If you do not, then click the relevant link below to understand.

The Perl CGI
When data is sent from the client, when it arrives in the server computer, it meets your script first. It is up to you now to extract the data in whatever form it came in and send to the email box or database server. Perl has a module called the CGI module. This module will extract that data for you. However, you need to know how to use the CGI. The CGI coding should be placed above the connect() function block, below the “use Mysql” and “Use strict” statements. CGI stands for Common Gateway Interface. I say more about that in the next part of the series.

Well, we have seen quite some good stuff; let us take a break here and continue in the next part of the series.


Related Links

Web Development Basics with Perl and MySQL
Perl Validation of HTML Form Data
Page Views with Ajax and Perl and MySQL
Web Live Text Chart Application using Perl and MySQL
Search Within a Site using Perl and MySQL
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course



Become the Writer's Follower
Send the Writer a Message