Using a straightforward, step-by-step approach, each lesson in this book builds on the previous ones, enabling you to learn the essentials of PHP scripting, MySQL databases, and the Apac
Trang 1applications using the three leading open-source Web development technologies.
Using a straightforward, step-by-step
approach, each lesson in this book builds on the previous ones, enabling you to learn the essentials of PHP scripting, MySQL databases, and the Apache Web server from the ground
Trang 2up.
Trang 3
• Table of Contents
• Index
Sams Teach Yourself PHP, MySQL® and Apache All in One
Trang 4Basic Security Guidelines
Introducing the MySQL Privilege System Working with User Privileges
Summary
Q&A
Workshop
Chapter 2 Installing and Configuring Apache Current and Future Versions of Apache Choosing an Installation Method
Building PHP on Linux/Unix with Apache Installing PHP Files on Windows
Trang 6Chapter 9 Working with Forms
Creating a Simple Input Form
Accessing Form Input with User-Defined Arrays Combining HTML and PHP Code on a Single Page Using Hidden Fields to Save State
Trang 7
Understanding the Image-Creation Process Necessary Modifications to PHP
Using the INSERT Command
Using the SELECT Command
Using WHERE in Your Queries
Summary
Q&A
Workshop
Chapter 16 Interacting with MySQL Using PHP Connecting to MySQL with PHP
Working with MySQL Data
Summary
Trang 8Workshop
Part V Basic Projects
Chapter 17 Managing a Simple Mailing List Developing the Subscription Mechanism
Displaying Items
Summary
Workshop
Chapter 21 Creating a Shopping Cart Mechanism Planning and Creating the Database Tables Integrating the Cart with Your Storefront
Payment Methods and the Checkout Sequence Summary
Trang 10Part VI Administration and Fine Tuning
Chapter 26 Apache Performance Tuning and Virtual Hosting Scalability Issues
Using the FLUSH Command
Using the SHOW Command
Trang 11Windows Installation
Troubleshooting
Index
Trang 12Copyright © 2004 by Sams Publishing
All rights reserved No part of this book shall be reproduced,stored in a retrieval system, or transmitted by any means,
electronic, mechanical, photocopying, recording, or otherwise,without written permission from the publisher No patent
liability is assumed with respect to the use of the informationcontained herein Although every precaution has been taken inthe preparation of this book, the publisher and author assume
no responsibility for errors or omissions Nor is any liabilityassumed for damages resulting from the use of the informationcontained herein
regarded as affecting the validity of any trademark or servicemark
Warning and Disclaimer
Trang 13as accurate as possible, but no warranty or fitness is implied.The information provided is on an "as is" basis The author andthe publisher shall have neither liability nor responsibility to anyperson or entity with respect to any loss or damages arisingfrom the information contained in this book or from the use ofthe CD or programs accompanying it
Bulk Sales
Sams Publishing offers excellent discounts on this book whenordered in quantity for bulk purchases or special sales For
Trang 15Lead author
Contributing authors
Trang 16Julie C Meloni is the technical director for i2i Interactive
(http://www.i2ii.com), a multimedia company located in LosAltos, California She's been developing Web-based applicationssince the Web first saw the light of day and remembers the
excitement surrounding the first GUI Web browser She has
authored several books and articles on Web-based
programming languages and database topics, and you can findtranslations of her work in several languages, including Chinese,Italian, Portuguese, Polish, and even Serbian
Trang 17Matt Zandstra is a writer and consultant specializing in server
programming With his business partner, Max Guglielmino, heruns Corrosive (http://www.corrosive.co.uk), a technical agencythat plans, designs and builds Internet applications Matt is
interested in all aspects of object-oriented programming, and iscurrently exploring enterprise design patterns for PHP 5 When
he is not reading, writing, or thinking about coding in PHP andJava, Matt shoots alien invaders in the park with his four-year-old daughter, Holly He lives by the sea in Brighton, Great
Britain, with his partner Louise McDougall, and their childrenHolly and Jake
Daniel López Ridruejo is the founder of BitRock, a technology
company providing multiplatform installation and managementtools for a variety of commercial and open source software
products Previously, he was part of the original engineeringteam at Covalent Technologies, Inc., which provides Apachesoftware, support, and services for the enterprise He is theauthor of several popular Apache and Linux guides, the
Danmarks Tekniske Universitet Daniel is a member of the
Apache Software Foundation
Trang 18The Apache Foundation, the PHP Group, and MySQL AB deservemuch more recognition than they ever get for creating thesesuper products that drive a great portion of the Web
in seeing this through! Thanks as always to everyone at i2i
Interactive for their never-ending support and encouragement
Trang 19As the reader of this book, you are our most important critic
and commentator We value your opinion and want to know
what we're doing right, what we could do better, what areasyou'd like to see us publish in, and any other words of wisdomyou're willing to pass our way
You can email or write me directly to let me know what you did
or didn't like about this bookas well as what we can do to makeour books stronger
Please note that I cannot help you with technical problems
related to the topic of this book, and that due to the high
volume of mail I receive, I might not be able to reply to every message.
When you write, please be sure to include this book's title andauthor as well as your name and phone or email address I willcarefully review your comments and share them with the authorand editors who worked on the book
Email: opensource@samspublishing.com
Mail: Mark Taber
Associate Publisher Sams Publishing
800 East 96th Street Indianapolis, IN 46240 USA
Trang 20For more information about this book or others from Sams
Publishing, visit our Web site at www.samspublishing.com Typethe ISBN (excluding hyphens) or the title of the book in theSearch box to find the book you're looking for
Trang 21Welcome to Sams Teach Yourself PHP, MySQL, and Apache All
in One! This book combines the lessons found in Sams Teach Yourself Apache 2 in 24 Hours, Sams Teach Yourself PHP in 24 Hours, and Sams Teach Yourself MySQL in 24 Hours, along with
administering the MySQL relational database system The
overall goal of the book is to provide you with the foundationyou need to understand how seamlessly these technologiesintegrate with one another, and to give you practical knowledge
of how to integrate them
Trang 22This book is geared toward individuals who possess a generalunderstanding of the concepts of working in a Web-based
development environment, be it Linux/Unix or Windows
Installation and configuration lessons assume that you havefamiliarity with your operating system and the basic methods ofbuilding (on Linux/Unix systems) or installing (on Windows
systems) software
The lessons that delve into programming with PHP assume noprevious knowledge of the language, but if you have experiencewith other programming languages, such as C or Perl, you willfind the going much easier Similarly, if you have worked withother databases, such as Oracle or Microsoft SQL Server, youwill have a good foundation for working through the MySQL-related lessons
The only real requirement is that you understand static Webcontent creation with HTML If you are just starting out in theworld of Web development, you will still be able to use this
book, although you should consider working through an HTMLtutorial If you are comfortable creating basic documents andcan build a basic HTML table, you will be fine
Trang 23This book is divided into seven parts, corresponding to
particular topic groups The lessons within each part should beread one right after another, with each lesson building on theinformation found in those before it:
Part I, "Laying the Groundwork," walks you through the
installation and configuration of MySQL, Apache, and PHP.You'll need to complete the lessons in Part I before moving
on, unless you already have access to a working installation
of these technologies Even if you don't need to install andconfigure MySQL, Apache, and PHP in your environment,you should still skim these lessons so that you understandthe basics
Part II, "PHP Language Structure," is devoted to teachingyou the basics of the PHP language, including structural
elements such as arrays and objects The examples will getyou in the habit of writing code, uploading it to your server,and testing the results
Part III, "Getting Involved with the Code," consists of
lessons that cover intermediate-level application-development topics, including working with forms and files,restricting access, and completing other small projects
designed to introduce a specific concept
Part IV, "PHP and MySQL Integration," contains lessons
devoted to working with databases in general, such as
database normalization, as well as using PHP to connect toand work with MySQL Included is a basic SQL primer, whichalso includes MySQL-specific functions and other
information
Trang 24performing a particular task using PHP and MySQL,
integrating all the knowledge gained so far Projects include
an address book, a discussion forum, and a basic onlinestorefront, among others
MySQL 4.1
If you find that you are already familiar with a topic, you canskip ahead to the next lesson However, in some instances,
lessons refer to specific concepts learned in previous chapters,
so be aware that you might have to skim a skipped lesson sothat your development environment remains consistent with thebook
At the end of each chapter, a few quiz questions test how wellyou've learned the material Additional activities provide
another way to apply the information learned in the lesson andguide you toward using this newfound knowledge in the nextchapter
Trang 25This book uses different typefaces to differentiate between codeand plain English, and also to help you identify important
concepts Throughout the lessons, code, commands, and textyou type or see onscreen appear in a computer typeface New
terms appear in italics at the point in the text where they are
defined Additionally, icons accompany special blocks of
information:
A "By the Way" presents an interesting piece of information related to the current topic.
A "Did You Know" offers advice or teaches an easier method for performing a task.
A "Watch Out" warns you about potential pitfalls and explains how
to avoid them.
Trang 26CHAPTER 1 Installing and Configuring MySQL
CHAPTER 2 Installing and Configuring Apache
CHAPTER 3 Installing and Configuring PHP
Trang 27How to install MySQL
Basic security guidelines for running MySQL
How to work with the MySQL user privilege system
Trang 28The installation instructions in this chapter refer to MySQL
4.0.15, which is the current production version of the software.This version number can be read as minor release number 15 ofthe major version 4.0 software MySQL AB, the company
responsible for creating and distributing MySQL, uses minorrelease numbers for updates containing security enhancements
or bug fixes Minor releases do not follow a set release
schedule; when enhancements or fixes are added to the codeand thoroughly tested, MySQL AB releases a new version, with
a new minor version number
It is possible that by the time you purchase this book, the minorversion number will have changed, to 4.0.16 or beyond If that
during the time you are reading this book, but no installationchanges are noted in the changelog, simply make a mental noteand substitute the new version number wherever it appears inthe installation instructions and accompanying figures
Trang 29For instructions on installing MySQL from the CD, please refer to Appendix A, "Installing MySQL, Apache, and PHP from the CD- ROM."
Trang 30binary distribution This method requires gunzip and tar to
uncompress and unpack the distribution, and also requires the
ability to create groups and users on the system The first
Trang 31process has you adding a group and a user and unpacking thedistribution, as follows:
Replace VERSION-OS in the filename with the actual version you downloaded For example, the current MySQL 4.0 Linux/i386 binary is called mysql-max-4.0.15-pc-linux-i686.tar.gz
Trang 32Installation" section
Trang 33personal Windows machines just to get a feel for working withthe database before deploying MySQL in a production
environment
If you have the tools and skills to compile your own Windows binary files, the Cygwin source code is also available from MySQL
AB Follow the instructions contained in the source distribution, to build your own executable files.
Jumping right into the installation sequence, assuming you havedownload the Windows installer from the MySQL AB Web site,follow these steps:
1 Extract the contents of the zip file into a temporary directory and find the setup.exe file, and then double- click it to start the installation You will see the first
Trang 34screen of the installation wizard, as shown in Figure 1.1 Click Next to continue.
information and note anything relevant to your situation, andthen click Next to continue
Figure 1.2 Step 2 of the MySQL installation wizard Note any relevant information before
continuing.
Trang 35MySQL in the default location, click Next to continue Otherwise,click Browse and navigate to the location of your choice, andthen click Next to continue
Figure 1.3 Step 3 of the MySQL installation wizard Select an installation location.
Trang 36methodTypical, Compact, or Custom (see Figure 1.4) TheCustom option allows you to select elements of MySQL to
install, such as documentation and help files Select Typical asthe installation method, and click Next to continue
Figure 1.4 Step 4 of the MySQL installation
wizard Select an installation type.
Trang 37a confirmation of completion, as in Figure 1.5 Click Finish tocomplete the setup process
Figure 1.5 MySQL has been installed.
Trang 38Figure 1.6 A directory listing of MySQL
applications.
Trang 39Windows users who are just getting started with MySQL If youdouble-click this file, it will start the MySQL server and place astoplight icon in your taskbar
When you start WinMySQLadmin for the first time, you will beprompted for a username and password (see Figure 1.7) Theapplication will create the initial MySQL user account on a
Windows system
Figure 1.7 Creating the initial MySQL account.
Trang 40provides an easy way to maintain and monitor your new server
Figure 1.8 WinMySQLadmin started and ready for
action.
Trang 41information, such as IP address and machine name The tabsacross the top allow you to view system information and editMySQL configuration options
For example, if you select the Variables tab, as shown in Figure1.9, you can also view server configuration information Thisinformation is similar to the output of the MySQL SHOW
VARIABLES command
Figure 1.9 Server configuration information.
To shut down the MySQL server and/or the WinMySQLadmintool, right-click again on the stoplight icon in your taskbar andselect the appropriate option (stop or start) As long as theMySQL server is running, you can run additional applicationsthrough a console window, such as the MySQL monitor