Psy 422 Week 6 - Databases and CGI

This week and next we introduce some tools that you may need for your project. Our introduction to each of these topics will necessarily be brief and incomplete. If you need to make more extensive use of any of these methods for your project, you will need to consult other sources for more complete information. Start with the links on the "Resources" page on the course server.

When you collect information via web pages, you will need to store that information somehow. The FormMail program gave you one option for collecting data (although not a very good one) by emailing it to yourself. Another option is to use a CGI program to store the data in a text file on the server. Finally, we could store the data in a database on the server, using either a CGI program or an embedded scripting tool like PHP to access the database.

Here is the plan for this week:

Next week we will try to fill in some of the puzzle further by looking at:


Password-protecting web pages

Read this brief guide to using passwords for web pages.
1. Then create a web page that requires the username guest and the password welcome to access. Link the password-protected page from your home page so that I can take a look at it.

Using Perl to save data to a file

Perl is a powerful programming/scripting language for text manipulation, and therefore is frequently used for server-side scripting, such as processing data from XHTML forms. We will delve more deeply into Perl later, but for now we will look at one example of using a Perl CGI script to save form data.

CGI stands for Common Gateway Interface, and it is a way for programs (such as Perl scripts) to interact with web pages. When a Perl script (for example) is used through the CGI, the data posted from a web form is directed to the standard input of the script, and the script's standard output is directed to the web browser.

The XHTML page http://sheu-hp.psy.depaul.edu/psy422/perl-cgi-forms1.html uses the perl program http://sheu-hp.psy.depaul.edu/cgi-bin/save-form-data-as-csv.cgi to write the form data to a text file as comma-separated values that can be imported by Excel. The file where the data are written is /tmp/surveydata.csv.

2. Copy the above XHTML and CGI files to your own public_html and cgi-bin directories. Then edit them to save form data in a file in your cgi-files directory. Tell me the name of the file so that I can take a look at it, and describe it.

Using a Mysql database

A database is a system for storing information in a way that makes it possible to retrieve, search, and manipulate the data easily. A database is composed of tables, each of which contains one record on each row, and a variable number of fields within each record. Each field can contain a different type of data, such as a date, a number, a string, a text, or even soundfiles or images.

SQL (Structured Query Language) is the standard language for most databases, including Microsoft Access, Oracale, and the open-source database system we will be using, Mysql. This means that once you learn to use one of these databases, you can probably learn to use all the others much more easily. We will practice using SQL commands to create, access, and modify tables in a database.

Mysql has its own system of user accounts and passwords for accessing its databases. Only the root account can create new databases or new user accounts. I have created a database for each student in the class (identified by your user name) for you to create tables in. There is also a database called test that anyone can read and write to. It is a good place to try things out when you are first getting started. I have created a few tables in the "test" database as examples to get us started.

Work through the commands listed in mysql-notes.html to practice using Mysql from the command line (in a telnet window).
3. Then create a table in your own database and place some data in it. Tell me the name of your table so that I can take a look at it, and describe it.

Using PHP to retrieve information from a Mysql database via a web page

Many web pages dynamically provide content in response to user requests by using databases. A typical scheme would be to use an XHTML form to take the user's request for data, then construct a SQL query to get information from a database and display that information in a web page. One option for doing this is a Perl CGI script. Another is PHP - a scripting language that is embedded within an XHTML document rather than using separate CGI programs.

The first step in using PHP for such purposes is to be able to use PHP within a web page to make SQL queries of a Mysql database. Take a look at this Example of using PHP to access a Mysql database on the course server (including looking at the source code on the server). Note that the file name of the web page ends in .php rather than .html. This is how the server knows to use the PHP engine to execute the embedded PHP scripts.

PHP scripts are executed by the server and the results inserted into the web page before the XHTML is sent to the browser. As a result, you can not use "view source" in your browser to see the PHP code. You must telnet into the server and look at the ".php" file directly, rather than through a browser.

4. Copy the above .php file to your public_html directory, and edit is so that it displays data from the table you created in your Mysql database. Link it from your home page so that I can take a look at it, and describe what it does.

Valid XHTML 1.0!