Datasheet

18
Part I: Developing a Web Database Application Using PHP and MySQL
Advantages of the relationship
MySQL and PHP as a pair have several advantages:
They’re free. It’s hard to beat free for cost-effectiveness.
They’re Web oriented. Both were designed specifically for use on Web
sites. Both have a set of features focused on building dynamic Web sites.
They’re easy to use. Both were designed to get a Web site up quickly.
They’re fast. Both were designed with speed as a major goal. Together
they provide one of the fastest ways to deliver dynamic Web pages to
users.
They communicate well with one another. PHP has built-in features
for communicating with MySQL. You don’t need to know the technical
details; just leave it to PHP.
How MySQL and PHP work together
PHP provides the application part, and MySQL provides the database part of
a Web database application. You use the PHP language to write the programs
that perform the application tasks. PHP can be used for simple tasks (such as
displaying a Web page) or for complicated tasks (such as accepting and veri-
fying data that a user typed into an HTML form). One of the tasks that your
application must do is move data into and out of the database — and PHP
has built-in features to use when writing programs that move data into and
out of a MySQL database.
PHP statements are embedded in your HTML files with PHP tags. When the
task to be performed by the application requires storing or retrieving data,
you use specific PHP statements designed to interact with a MySQL database.
You use one PHP statement to connect to the correct database, telling PHP
where the database is located, its name, and the password needed to connect
to it. The database doesn’t need to be on the same machine as your Web site;
PHP can communicate with a database across a network. You use another
PHP statement to send an SQL message to MySQL, giving MySQL instructions
for the task you want to accomplish. MySQL returns a status message that
shows whether it successfully performed the task. If a problem came up, it
returns an error message. If your SQL message asked to retrieve some data,
MySQL sends the data that you asked for, and PHP stores it in a temporary
location where it’s available to you.
You then use one or more PHP statements to complete the application task.
For instance, you can use PHP statements to display data that you retrieved.
Or you might use PHP statements to display a status message in the browser,
informing the user that the data was saved.
05_527580-ch01.indd 1805_527580-ch01.indd 18 11/3/09 8:50 PM11/3/09 8:50 PM