SQL in perl on the Web

Revision as of 10:50, 19 July 2012 by Connor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In order to create database backed website you need each of the following:

  • a web server,
  • a scripting language,
  • a database.

The following is based on Windows / Apache /Perl / MySQL

  • Install Perl.
  • Use ppm to install the MySQL module. From the Command Prompt
PPM>install DBD-Mysql
  • Install Apache
  • Start your Apache server: C:\Program Files\Apache Group\Apache\Apache.exe or C:\Program Files\Apache Group\Apache2\bin\Apache.exe
  • Create a file called C:\Program Files\Apache Group\Apache\cgi-bin\cia.htm the text of the program is shown below
  • The given program connects to a MySQL server running at Napier University
  • Admire your work: Visit http://localhost/cgi-bin/cia.htm

use CGI qw(:standard);
use Mysql;

print header();

#Import the CGI parameters into the $Q structure

#Connect to Andrew's MySQL server at Napier University
my $dbh = DBI->connect("dbi:mysql:database=gisq;host=pc236nt.napier.ac.uk;port=3306",
                       "scott", "tiger");

my $sth = $dbh->prepare("SELECT DISTINCT region FROM cia");
print "<h1>Here are some regions</h1>\n";
while (my ($region) = $sth->fetchrow_array()){
  print "<a href='?region=$region'>$region<a><br/>\n";

if ($Q::region) {
  print "<h1>Here are the countries of $Q::region</h1>";
  my $sth = $dbh->prepare("SELECT name, population FROM cia where region=?");
  while (my ($name,$pop) = $sth->fetchrow_array()){
    print "$name $pop<br/>\n";


This is what it should look like