Read an Excerpt
PHP and MySQL for Dummies with CDROM
By Janet Valade
John Wiley & Sons ISBN: 0-7645-1650-7
Chapter One Introduction to PHP and MySQL
In This Chapter
* Finding out what a Web database application is
* Taking a look at PHP
* Discovering how MySQL works
* Finding out how PHP and MySQL work together
So you need to develop an interactive Web site. Perhaps your boss just put you in charge of the company's online product catalog. Or you want to develop your own Web business. Or your sister wants to sell her paintings online. Or you volunteered to put up a Web site open only to members of your circus acrobats' association. Whatever your motivation may be, you can see that the application needs to store information (for instance, information about products, information about paintings, member passwords), thus requiring a database. You can also see that the application needs to interact dynamically with the user (for instance, the user selects a product to view, the user enters membership information). This type of Web site is called a Web database application.
You have created static Web pages before, using HTML, but creating an interactive Web site is a new challenge, as is designing a database. You asked three computer gurus you know what you should do. They said a lot of things you didn't understand, but among the technical jargon, you heard "quick" and "easy" and "free" mentioned in the same sentence as PHP and MySQL. Now you want to know moreabout using PHP and MySQL to develop the Web site you need.
PHP and MySQL work together very well. It's a dynamic partnership. In this chapter, you find out the advantages of each, how each one works, and how they work together to produce a dynamic Web database application.
What Is a Web Database Application?
An application is a program or a group of programs designed for use by an end user (for example, customers, members, circus acrobats, and so on). If the end user interacts with the application using a Web browser, the application is a Web-based or Web application. If the Web application requires the long-term storage of information, using a database, it is a Web database application. This book provides you with the information you need to develop a Web database application that can be accessed using Web browsers such as Internet Explorer and Netscape.
A Web database application is designed to help a user accomplish a task. It can be a simple application that displays information in a browser window (for example, it displays current job openings when the user selects a job title) or a complicated program with extended functionality (for example, the book-ordering application at Amazon or the bidding application at eBay).
Not surprisingly, a Web database application consists of an application and a database - just two pieces:
The database is the long-term memory of your Web database application. The application can't fulfill its purpose without the database. However, the database alone is not enough.
The application piece is the program or group of programs that perform the tasks. Programs create the display that the user sees in the browser window; they make your application interactive by accepting and processing information the user typed in the browser window; and they store information in the database and get information out of the database. The database is useless unless you can move data in and out.
The Web pages you have previously created using HTML alone are static, meaning the user can't interact with the Web page. All users see the same Web page. Dynamic Web pages, on the other hand, allow the user to interact with the Web page. Different users might see different Web pages. For instance, one user looking at a furniture store's online product catalog might choose to view information about the sofas, whereas another user might choose to view information about coffee tables. To create dynamic Web pages, you must use another language, in addition to HTML.
The core of a Web database application is the database, the long-term memory that stores information for the application. A database is an electronic file cabinet that stores information in an organized manner so that you can find it when you need it. After all, storing information is pointless if you can't find it. A database can be small, with a simple structure - for example, a database containing the titles and authors' names of all the books you own. Or a database can be huge, with an extremely complex structure - such as the database Amazon must have to hold all its information.
The information you store in the database comes in many varieties. A company's online catalog requires a database to store information about all the company's products. A membership Web site requires a database to store information about members. An employment Web site requires a database (or perhaps two databases) to store information about job openings and information from resumes. The information you plan to store may be similar to information that is stored by Web sites all over the Internet - or information that is unique to your application.
Technically, the term database refers to the file or group of files that holds the actual data. The data is accessed using a set of programs called a Database Management System (DBMS). Almost all DBMSs these days are Relational Database Management Systems (RDBMSs), in which data is organized and stored in a set of related tables.
In this book, MySQL is the RDBMS used because it is particularly well suited for Web sites. MySQL and its advantages are discussed in the section, "MySQL, My Database," later in this chapter. You can find out about how to organize and design a MySQL database in Chapter 3.
The application: Moving data in and out of the database
For the database to be useful, you need to be able to move data into and out of it. Programs are your tools for this. Programs interact with the database to store and retrieve data. A program connects to the database and makes a request: "Take this data and store it in the specified location." Another program makes the request: "Find the specified data and give it to me." The application programs that interact with the database run when the user interacts with the Web page. For instance, when the user clicks the Submit button after filling in a Web form, a program processes the information in the form and stores it in a database.
MySQL, My Database
MySQL is a fast, easy-to-use RDBMS used for databases on many Web sites. Speed was the developers' main focus from the beginning. In the interest of speed, they made the decision to offer fewer features than their major competitors (for instance, Oracle and Sybase). However, even though MySQL is less full-featured than its commercial competitors, it has all the features needed by the large majority of database developers. It is easier to install and use than its commercial competitors, and the difference in price is strongly in MySQL's favor.
MySQL is developed, marketed, and supported by MySQL AB, a Swedish company. The company licenses it two ways:
Open source software: MySQL is available via the GNU General Public License (GPL) for no charge. Anyone who can meet the requirements of the GPL can use the software for free. If you're using MySQL as a database on a Web site (the subject of this book), you can use MySQL for free, even if you're making money with your Web site.
Commercial license: MySQL is available with a commercial license for those who prefer it to the GPL. If a developer wants to use MySQL as part of a new software product and wants to sell the new product, rather than release it under the GPL, the developer needs to purchase a commercial license. The fee is very reasonable. Finding technical support for MySQL is not a problem. You can join one of several e-mail discussion lists offered on the MySQL Web site at mysql. com. You can even search the e-mail list archives, which contain a large knowledge base of MySQL questions and answers. If you're more comfortable getting commercial support, MySQL AB offers technical support contracts - five support levels, ranging from direct e-mail support to phone support, at five price levels.
Advantages of MySQL
MySQL is a popular database with Web developers. Its speed and small size make it ideal for a Web site. Add to that the fact that it's open source, which means free, and you have the foundation of its popularity. Here is a rundown of some of its advantages:
It's fast: The main goal of the folks who developed MySQL was speed. Consequently, the software was designed from the beginning with speed in mind.
It's inexpensive: MySQL is free under the open source GPL license, and the fee for a commercial license is very reasonable.
It's easy to use: You can build and interact with a MySQL database using a few simple statements in the SQL language, the standard language for communicating with RDBMSs. Check out Chapter 4 for the lowdown on the SQL language.
It can run on many operating systems: MySQL runs on a wide variety of operating systems - Windows, Linux, Mac OS, most varieties of Unix (including Solaris, AIX, and DEC Unix), FreeBSD, OS/2, Irix, and others.
Technical support is widely available: A large base of users provides free support via mailing lists. The MySQL developers also participate in the e-mail lists. You can also purchase technical support from MySQL AB for a very small fee.
It's secure: MySQL's flexible system of authorization allows some or all database privileges (for example, the privilege to create a database or delete data) to specific users or groups of users. Passwords are encrypted.
It supports large databases: MySQL handles databases up to 50 million rows or more. The default file size limit for a table is 4GB (gigabytes), but you can increase this (if your operating system can handle it) to a theoretical limit of 8 million terabytes.
It's customizable: The open source GPL license allows programmers to modify the MySQL software to fit their own specific environments.
It's memory-efficient: MySQL is written and thoroughly tested to prevent memory leaks.
How MySQL works
The MySQL software consists of the MySQL server, several utility programs that assist in the administration of MySQL databases, and some supporting software that the MySQL server needs, but that you don't need to know about. The heart of the system is the MySQL server.
The MySQL server is the manager of the database system. It handles all your database instructions. For instance, if you want to create a new database, you send a message to the MySQL server that says "create a new database and call it newdata." The MySQL server then creates a subdirectory in its data directory, names the new subdirectory newdata, and puts the necessary files with the required format into the newdata subdirectory. In the same manner, to add data to that database, you send a message to the MySQL server, giving it the data and telling it where you want the data to be added. You find out how to write and send messages to MySQL in Part II of this book.
Before you can pass instructions to the MySQL server, it must be running. The MySQL server is usually set up so that it starts when the computer starts and continues running all the time. This is the usual setup for a Web site. However, it's not necessary to set it up to start when the computer starts. If you need to, you can start it manually whenever you want to access a database. When it's running, the MySQL server listens continuously for messages that are directed to it.
Communicating with the MySQL server
All your interaction with the database is done by passing messages to the MySQL server. You can send messages to the MySQL server several ways, but this book focuses on sending messages using PHP. The PHP software has specific statements that you use to send instructions to the MySQL server.
The MySQL server must be able to understand the instructions that you send it. You communicate using the Structured Query Language (SQL), a standard language understood by many RDBMSs. The MySQL server understands SQL. PHP does not understand SQL, but it doesn't need to. PHP just establishes a connection with the MySQL server and sends the SQL message over the connection. The MySQL server interprets the SQL message and follows the instructions. The MySQL server sends a return message, stating its status and what it did (or reporting an error if it was unable to understand or follow the instructions). For the lowdown on how to write and send SQL messages to MySQL, check out Part II of this book.
PHP, a Data Mover
PHP is a scripting language designed specifically for use on the Web. PHP is your tool for creating dynamic Web pages. As a special-purpose language, PHP does not need to include many of the features required in a general, all-purpose programming language. Consequently, it is much simpler than many languages, such as C or Java, and contains only the features that are most useful for Web sites.
PHP stands for PHP: Hypertext Preprocessor. In its early development by a guy named Rasmus Lerdorf, it was called Personal Home Page tools. When it developed into a full-blown language, the name was changed to be more in line with its expanded functionality.
The PHP language's syntax is similar to the syntax of C, so if you have experience with C, you will be comfortable with PHP. PHP is actually simpler than C because it doesn't use some of the more difficult concepts of C. PHP also doesn't include the low-level programming capabilities of C because PHP is designed to program Web sites and doesn't require those capabilities.
PHP is particularly strong in its ability to interact with databases. PHP supports pretty much every database you've ever heard of (and some you haven't).
Excerpted from PHP and MySQL for Dummies with CDROM by Janet Valade Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.