IEEM 230

Industrial Data systems

 

CGI's: How to connect to a Database ?

 

In this lab, we look at how to connect the internet browser (e.g. Internet Explorer, or Netscape Navigator) to a Database system. In our case, we shall use the most popular architecture for such connectivity: we shall make the web server to be the same computer as our Database client. Thus, we need two things.

First, we need a mechanism for data to be transferred to and from our web client computer to the web server.
But we already know that we can use FORMs and CGI programs for such data exchange.

Second, we need to know how to make our CGI programs to connect to the Database server, and to exchange information with the DB server. "Exchange" here means (a) the ability to log into our Database server account; (b) The ability to send an SQL query to the DB server; and (c) To receive the result of our query, which is usually a table, back into our CGI program.

To do so, we shall use a special program to execute out CGI's. This program is an extension of perl, and is called sybperl.

PERL can link to SYBASE by using a special set of functions, which are provided to you through sybperl . SYBPERL contains all of perl, plus SYBASE linking functions. The following example will show you how SYBPERL can be used to:

(a) Connect to a SYBASE server;

(b) Send some SQL commands to the SYBASE server;

(c) How to receive the output from the SQL command;

and finally

(d) How to compose it all into a nice, HTML format that can be displayed as the result of submitting the form, to the visitor of your web-site.

 

First a form to get some information. Note that this form cannot execute. You may copy its source into your public_html directory, and modify the "ACTION=..." portion to make it work.

Next, you need to write a SYBPERL script NOTE: the script is in sybperl, not perl which is the CGI program. When you submit the form, a CGI-script similar to this one gets executed.

Notes:
1. Note that if you are connecting to SYBASE, your CGI program must be written in sybperl , not perl. The first line is:
#!/usr/local/sybase/sybperl

2. Within sybperl, you can use all perl functions, as well as the methods shown to connect to Sybase, and run queries in Sybase.

3. Notice that a new type of data structure is used by the subroutine ReadParse . It is an associative list, or a hash list, which is like a 2-column array. The keyword of the first column has the value given by the second column. This is a very nice way to refer to inputs that are identified by names (such as SID, age, etc).

4. My CGI-script assumes the existence of a table (in my account) of a table called EMPLOYEE which has the following attributes: Lname and SSN. There may be more attributes, of course! You must replace these names with your own table names and attribute names.