. 6
( 132 .)


Introduction xli

Figure I-3: create_entry.php after submission

This shouldn™t be too tough. You already know that the file needs to include
dbconnect.php. Other than that, we™ve already mentioned that databases store
information in tables. Each row of the table contains information on a specific
person who signed the guestbook, so to view all the information the page needs to
retrieve and print out every row of data. Here™s the script that can do it (you should
notice that it™s pretty sparse):

<?php include(“dbconnect.php”); ?>

<h2>View My Guest Book!!</h2>


$result = mysql_query(“select * from guestbook”)
or die(mysql_error());
while ($row = mysql_fetch_array($result))
xlii Introduction

echo “<b>Name:</b>”;
echo $row[“name”];
echo “<br>\n”;
echo “<b>Location:</b>”;
echo $row[“location”];
echo “<br>\n”;
echo “<b>Email:</b>”;
echo $row[“email”];
echo “<br>\n”;
echo “<b>URL:</b>”;
echo $row[“url”];
echo “<br>\n”;
echo “<b>Comments:</b>”;
echo $row[“comments”];
echo “<br>\n”;
echo “<br>\n”;
echo “<br>\n”;

<h2><a href=”sign.php”>Sign My Guest Book!!</a></h2>

The query in the preceding code asks MySQL for every row in the database. Then
the script enters a loop. Each row in the database is loaded into the variable $row,
one row at a time. Rows continue to be accessed until none is left. At that time, the
script drops out of the while loop.
As it works through the loop, each column in that row is displayed. For example,
the following code prints out the email column for the row being accessed:

echo $row[“email”]

When run, the simple script at the beginning of this section prints out every row
in the database. Figure I-4 shows what the page will look like.
Introduction xliii

Figure I-4: view.php

And that about does it for our first application.

Again, we strongly recommend against putting the application discussed in this
introduction anywhere that the general public can get to it. If you want a guest-
book, use the application made exclusively for this book, which you find in Chapter
8. We call this application Guestbook 2003. But we cover a lot of ground and pre-
sent a lot of information before we get there.
We hope you enjoy the read!
Part I
Working with MySQL
Database Design with MySQL
The Structured Query Language for Creating
and Altering Tables
The Structured Query Language for Inserting,
Editing, and Selecting Data
Chapter 1

Database Design with

— Identifying the problems that led to the creation of the relational database

— Learning the normalization process

— Examining advanced database concepts

THE BULK OF THIS CHAPTER is for those of you who have made it to the early twenty-
first century without working with relational databases. If you™re a seasoned data-
base pro, having worked with Oracle, Sybase, or even something like Microsoft
Access or Paradox, you may want to skip this little lesson on database theory.
However, we do suggest that you look at the final section of this chapter, where we
discuss some of MySQL™s weirder points. MySQL™s implementation of SQL is incom-
plete, so it might not support something you want to use.

Why Use a Relational Database?
If you™re still here and are ready to read with rapt attention about database theory
and the wonders of normalization, you probably don™t know much about the his-
tory of the relational database. You may not even care. For that reason, I™ll keep this
very brief. Dr. E. F. Codd was a research scientist at IBM in the 1960s. A mathe-
matician by training, he was unhappy with the available models of data storage,
finding them all prone to error and redundancy. He worked on these problems and
then, in 1970, published a paper with the rousing title “A Relational Model of Data
for Large Shared Data Banks.” In all honesty, nothing has been the same since.
A programmer named Larry Ellison read the paper and started work on software
that could put Dr. Codd™s theories into practice. If you™ve been a resident of this
planet during the past 20 years, you may know that Ellison™s product and company
took the name Oracle and that he is now one of the richest individuals in the world.
His earliest product was designed for huge mainframe systems. Responding to mar-
ket demands over the years, Oracle, and many other companies that have sprung up
since, have designed systems with a variety of features geared toward a variety of
4 Part I: Working with MySQL

operating systems. Now relational databases are so common that you can get one
that runs on a Palm Pilot.
To understand why Dr. Codd™s theories have revolutionized the data-storage
world, it™s best to have an idea of what the troubles are with other means of data
storage. Take the example of a simple address book ” nothing too complex, just
something that stores names, addresses, phone numbers, emails, and the like. If you
have no persistent, running program to put this information into, the file system of
whatever OS you™re running becomes the natural choice for storage.
For a simple address book, a delimited text file can be created to store the infor-
mation. If the first row serves as a header and commas are used as delimiters, the
text file might look something like this:

Name, Addr1, Addr2, City, State, Zip, Phone, Email
Jay Greenspan, 211 Some St, Apt 2, San Francisco, CA, 94107,
4155551212, jay@not.real
Brad Bulger, 411 Some St, Apt 6, San Francisco, CA, 94109,
4155552222, brad@not.real
John Doe, 444 Madison Ave, , New York, NY, 11234, 2125556666,

This isn™t much to look at, but it is at least machine-readable. Using whatever
language you wish, you can write a script that opens this file and then parses the
information. You will probably want it in some sort of two-dimensional or associa-
tive array so that you™ll have some flexibility in addressing each portion of each
line of the file. Any way you look at it, there™s going to be a fair amount of code to
write. If you want this information to be sortable and queryable by a variety of cri-
teria, you™re going to have to write scripts that will, for instance, sort the list alpha-
betically by name or find all people within a certain area code. What a pain.
You might face another major problem if your data needs to be used across a
network by a variety of people. Presumably more than one person is going to need
to write information to this file. What happens if two people try to make changes at
once? For starters, it™s quite possible that one person will overwrite another™s
changes. To prevent this from happening, the programmer has to specify file lock-
ing if the file is in use. While this might work, it™s kind of a pain in the neck for the
person who gets locked out. Obviously, the larger the system gets the more unman-
ageable this all becomes.
What you need is something more robust than the file system ” a program or
daemon that stays in memory seems to be a good choice. Furthermore, you™ll need
a data-storage system that reduces the amount of parsing and scripting that the
programmer needs to be concerned with. No need for anything too arcane here. A
plain, simple table like Table 1-1 should work just fine.
Now this is pretty convenient. It™s easy to look at and if a running program
accesses this table it should happen pretty quickly. What else might this program
do? First, it should be able to address one row at a time without affecting the oth-
ers. That way, if two or more people want to insert information into this table they
Chapter 1: Database Design with MySQL 5

won™t be tripping over each other. It would be even spiffier if the program provided
a simple and elegant way to extract information from a table such as this. There
should be a quick way to find all of the people from California that doesn™t involve
parsing and sorting the file. Furthermore, this wondrous program should be able to
accept statements that describe what you want in a language very similar to
English. That way you can just say: “Give me all rows where the contents of the
state column equal CA.”
Yes, this program is great, but it isn™t enough. Major problems still need to be
dealt with. These problems, which we™ll discuss in the following pages, are the same
ones that made Dr. Codd write his famous paper, and the same ones that made Larry
Ellison a billionaire.

Blasted Anomalies
Dr. Codd™s goal was to have a model of information that was dependable. All of the
data-storage methods available to him had inherent problems. He referred to these
problems as anomalies. There are three types of anomalies: update, delete, and insert.

The update anomaly
Now that you can assume that a table structure can quickly and easily handle mul-
tiple requests, you need to see what happens when the information gets more com-
plex. Adding some more information to the previous table introduces some serious
problems (Table 1-2).
Table 1-2 is meant to store information for an entire office, not just a single per-
son. Since this company deals with other large companies, there will be times when
more than one contact will be at a single office location. For example, in Table 1-2
two contacts are present at 1121 43rd St. At first this may appear to be okay; you
can still get at all the information available relatively easily. The problem comes
when the BigCo Company decides to up and move to another address. In that case,
you™d have to update the address for BigCo in two different rows. This may not
sound like such an onerous task, but consider the trouble if this table has 3,000
rows instead of 3 ” or 300,000 for that matter. Someone, or some program, has to
make sure the data are changed in every appropriate place.
Another concern is the potential for error. It™s very possible that one of these
rows could be altered while the other one remained the same. Or, if changes are
keyed in one row at a time, it™s likely that somebody will introduce a typo. Then
you™d be left wondering if the correct address is 1121 or 1211.
The better way to handle this data is to take the company name and address and
put that information in its own table. This process of separating a table out into
multiple new tables is usually called decomposition. The two resulting tables will
resemble Table 1-3 and Table 1-4.
Now the information pertinent to BigCo is in its own table, Companies. If you
look at the next table (Table 1-4), Contacts, you™ll see that we™ve inserted another


name addr1 addr2 city state zip phone email

Jay Greenspan 211 Some St. Apt. 2 San Francisco CA 94107 4155558888
Brad Bulger 411 Some St. Apt. 6 San Francisco CA 94109 4155552222
nobody@ mysqlphpapps.com
John Doe 444 Madison Ave. New York NY 11234 2125556666
Part I: Working with MySQL


id company_name company_address contact_name contact_title phone email

1 BigCo Company 1121 43rd St. Jay Greenspan Vice President 4155551212
2 BigCo Company 1121 43rd St. Brad Bulger President 4155552222
3 LittleCo Company 4444 44th St. John Doe Lackey 2125556666

company_id company_name company_address

1 BigCo Company 1121 43rd St.
2 LittleCo Company 4444 44th St.


contact_id company_id contact_name contact_title phone email

1 1 Jay Greenspan Vice President 4155551212
2 1 Brad Bulger President 4155552222


. 6
( 132 .)