Chapter 12. PHP and Other Support for Database-driven HTML

Contents:

Several easy-to-use program HTML extensions provide support for
accessing MySQL and mSQL database servers within web pages. In this
chapter, we will start with W3-mSQL -- a mSQL-specific tool. We
will then show how to take the more database-independent approach
with PHP and two minor Perl extensions. The W3-mSQL scripting
language, Lite, lets you embed entire programs into an HTML file. A
CGI program executes the script and sends the result to the reader as
a dynamically created HTML
document.

Since W3-mSQL and the other extensions in this chapter use their own
scripting languages and hide all evidence of CGI, knowledge of the
previous chapters of this section is not necessary for this chapter.
However, understanding how CGI works, as well as having some prior
programming experience (Lite is similar to both C and Perl), can be
useful when reading this chapter.

12.1. Alternatives for Dynamic Content on the Web

The World Wide Web's first encounter with what we now call
Dynamic HTML was Server Side Includes (SSI). The idea
behind SSI is that there are certain common values, such as the
current date and time, that would be useful to include in an HTML
page but impracticable because they change so often. SSI provided a
method by which an HTML page could tell the server to insert a value
into the HTML page before sending it to the end user. That way the
value would always be current, but the creator of the page would not
have to continuously update it. Within an HTML page, a typical SSI
directive looks like this:

<!--#echo var="DATE_LOCAL" -->

The problem with SSI is that there is a very limited set of
information that the server can easily provide. Once you get past
date, time, and the ability to include other files there is not much
else available without seriously bloating the web server itself.

It quickly became apparent that if the web server itself did not
provide dynamic HTML, it could come from only two other sources. The
client -- that is, the web browser -- could interpret the
commands or some other program on the server machine could preprocess
the commands, outputting plain HTML to the end users.

The first road is what led to
JavaScript and
other similar technologies. With JavaScript, as with SSI, commands
are embedded within the HTML. Unlike SSI, the server does not touch
JavaScript commands; instead, the web browser handles them. This
method allows for much greater interaction with the user. For
instance, using JavaScript you may specify that an action take place
when the user moves the mouse over different parts of the screen. In
this way, it becomes possible to create a feeling of immediacy and
interactivity not otherwise possible. Following is an example of
typical JavaScript code:

<SCRIPT>
<! onMouseOver("do the jig"); -->
</script>

The problem with client-side solutions, such as JavaScript, is that
as soon as the client is finished downloading the page, the
connection with the server is lost. Very often there are resources on
the server machine, such as database servers, with which we would
like to interact. However, with client-side scripting it is usually
either impossible or impractical to communicate with the server or
any other remote machine after the page has loaded. This type of
functionality is best suited for a server side solution.

With a server-side interpreter, an HTML
document is examined before being sent to the end user. Some program,
usually a CGI program, looks for and executes programming code
embedded in the HTML. The advantage of this system is that you gain
all of the power of a CGI program while hiding much of the
complexity.

Consider a marine foundation that has a database containing
information about sharks. This database has vital statistics of the
various shark species, as well as filenames pointing to images of the
creatures. Creating a web interface to this database is an ideal
application of server-side interpreted HTML. All of the output pages
containing information about a particular shark will be formatted
similarly. In the few places where dynamic data from the database is
required, commands can be inserted which will be executed before the
user sees the page. You can even generate dynamic <IMG> tags
that show the desired pictures. Later in the chapter we will look at
how to implement this example using a variety of server-side
interpreters.