PHP and MySQL are popular open-source technologies that are ideal for quickly developing database-driven Web applications. PHP is a powerful scripting language designed to enable developers to create highly featured Web applications quickly and MySQL is a fast, reliable database that integrates well with PHP and is suited for dynamic Internet-based applications.
PHP and MySQL Web Development shows how to use these tools together to produce effective, interactive Web applications. It clearly describes the basics of the PHP language, explains how to set up and work with a MySQL database, and then shows how to use PHP to interact with the database and the server.
This practical hands-on guide includes numerous examples that demonstrate common tasks such as authenticating users, constructing a shopping cart, generating PDF documents and images dynamically, sending and managing email, facilitating user discussions, managing content, and connecting to Web services using XML.
The third edition of PHP and MySQL Web Development has been thoroughly updated, revised, and expanded to include coverage of PHP 5 -- including its new object model, better exception handling, and SimpleXML -- as well as features introduced in MySQL 5, such as stored procedures.
About the Authors
Luke Welling and Laura Thomson are partners in Tangled Web Design, an award-winning Web development firm that specializes in developing dynamic Web sites using PHP. Welling is a senior Web developer for MySQL AB, and Thomson is a lecturer in Web programming, software engineering, and e-commerce at RMIT University in Melbourne, Australia. Both are frequent speakers at leading open-source conferences around the world.
|Series:||Developer's Library Series|
|Product dimensions:||7.00(w) x 8.80(h) x 1.40(d)|
Table of Contents
I. USING PHP.
The Order Form. Processing the Form.
Embedding PHP in HTML.
Using PHP Tags. PHP Tag Styles. PHP Statements. Whitespace. Comments.
Adding Dynamic Content.
Calling Functions. The date() Function.
Accessing Form Variables.
Form Variables. String Concatenation. Variables and Literals.
Identifiers. User-Declared Variables. Assigning Values to Variables. Variable Types.
PHP's Data Types. Type Strength. Type Casting. Variable Variables.
Constants. Variable Scope. Operators.
Arithmetic Operators. String Operators. Assignment Operators. Comparison Operators. Logical Operators. Bitwise Operators. Other Operators.
Using Operators: Working Out the Form Totals. Precedence and Associativity: Evaluating Expressions. Variable Functions.
Testing and Setting Variable Types. Testing Variable Status. Reinterpreting Variables.
Control Structures. Making Decisions with Conditionals.
if Statements. Code Blocks. A Side Note: Indenting Your Code. else Statements. elseif Statements. switch Statements. Comparing the Different Conditionals.
Iteration: Repeating Actions.
while Loops. for Loops. do..while Loops.
Breaking Out of a Control Structure or Script. Next: Saving the Customer's Order.
2. Storing and Retrieving Data.
File Modes. Using fopen() to Open a File. Opening Files for FTP or HTTP. Problems Opening Files.
Writing to a File. Parameters for fwrite(). File Formats.
Closing a File. Reading from a File.
Opening a File for Reading: fopen(). Knowing When to Stop: feof(). Reading a Line at a Time: fgets(), fgetss(), and fgetcsv(). Reading the Whole File: readfile(), fpassthru(), file(). Reading a Character: fgetc(). Reading an Arbitrary Length: fread().
Other Useful File Functions.
Checking Whether a File Is There: file_exists(). Knowing How Big a File Is: filesize(). Deleting a File: unlink(). Navigating Inside a File: rewind(), fseek(), and ftell().
File Locking. Doing It a Better Way: Database Management Systems.
Problems with Using Flat Files. How RDBMSs Solve These Problems. Further Reading. Next.
3. Using Arrays.
Initializing Numerically Indexed Arrays. Accessing Array Contents. Using Loops to Access the Array.
Associative Arrays. Initializing an Associative Array. Accessing the Array Elements. Using Loops with each() and list().
Multidimensional Arrays. Sorting Arrays.
Using sort(). Using asort() and ksort() to Sort Associative Arrays. Sorting in Reverse.
Sorting Multidimensional Arrays.
User Defined Sorts. Reverse User Sorts.
Using shuffle(). Using array_reverse().
Loading Arrays from Files. Other Array Manipulations.
Navigating Within an Array: each, current(), reset(), end(), next(), pos(), and prev(). Applying Any Function to Each Element in an Array: array_walk(). Counting Elements in an Array: count(), sizeof(), and array_count_values(). Converting Arrays to Scalar Variables: extract().
Further Reading. Next.
4. String Manipulation and Regular Expressions.
Trimming Strings: chop(), ltrim(), and trim(). Formatting Strings for Presentation. Formatting Strings for Storage: AddSlashes() and StripSlashes().
Joining and Splitting Strings with String Functions.
Using explode(), implode(), and join(). Using strtok(). Using substr().
String Ordering: strcmp(),strcasecmp(), and strnatcmp(). Testing String Length with strlen().
Matching and Replacing Substrings with String Functions.
Finding Strings in Strings: strstr(), strchr(), strrchr(), stristr(). Finding the Position of a Substring: strpos(), strrpos(). Replacing Substrings: str_replace(), substr_replace().
Introduction to Regular Expressions.
The Basics. Character Sets and Classes. Repetition. Subexpressions. Counted Subexpressions. Anchoring to the Beginning or End of a String. Branching Matching Literal Special Characters. Summary of Special Characters. Putting It All Together for the Smart Form.
Finding Substrings with Regular Expressions. Replacing Substrings with Regular Expressions. Splitting Strings with Regular Expressions. Comparison of String Functions and Regular Expression Functions. Further Reading. Next.
5. Reusing Code and Writing Functions.
Cost. Reliability. Consistency.
Using require() and include().
Using require(). File Name Extensions and Require(). PHP Tags and require().
Using require() for Web Site Templates.
Using auto_prepend_file and auto_append_file. Using include().
Using Functions in PHP.
Calling Functions. Call to Undefined Function. Case and Function Names.
Why Should You Define Your Own Functions? Basic Function Structure.
Naming Your Function.
Parameters. Scope. Pass by Reference Versus Pass by Value. Returning from Functions. Returning Values from Functions. Code Blocks.
Recursion. Further Reading. Next.
6. Object-Oriented PHP.
Classes and Objects. Polymorphism. Inheritance.
Creating Classes, Attributes, Operations in PHP. Structure of a Class. Constructors.
Instantiation. Using Class Attributes. Calling Class Operations. Implementing Inheritance in PHP.
Overriding. Multiple Inheritance.
Designing Classes. Writing the Code for Your Class. Next.
II. USING MYSQL.
Tables. Columns. Rows. Values. Keys. Schemas. Relationships.
How to Design Your Web Database.
Think About the Real World Objects You Are Modeling. Avoid Storing Redundant Data. Use Atomic Column Values. Choose Sensible Keys. Think About the Questions You Want to Ask the Database. Avoid Designs with Many Empty Attributes. Summary of Table Types.
Web Database Architecture.
Further Reading. Next.
8. Creating Your Web Database.
Creating the Database.
Users and Privileges. Introduction to MySQL's Privilege System.
Principle of Least Privilege. Setting Up Users: The GRANT Command. Types and Levels of Privilege. The REVOKE Command. Examples Using GRANT and REVOKE.
Setting Up a User for the Web.
Logging Out As root.
Using the Right Database. Creating Database Tables.
What the Other Keywords Mean. Understanding the Column Types. Looking at the Database with SHOW and DESCRIBE.
MySQL Identifiers. Column Data Types.
Numeric Types. Further Reading. Next.
9. Working with Your MySQL Database.
Retrieving Data from the Database. Retrieving Data with Specific Criteria. Retrieving Data from Multiple Tables. Retrieving Data in a Particular Order. Grouping and Aggregating Data. Choosing Which Rows to Return.
Updating Records in the Database. Altering Tables After Creation. Deleting Records from the Database. Dropping Tables. Dropping a Whole Database. Further Reading. Next.
10. Accessing Your MySQL Database from the Web with PHP.
Freeing Up Resources. Creating and Deleting Databases.
Other PHP-Database Interfaces. Further Reading. Next.
11. Advanced MySQL.
The user Table. The db and host Tables. The tables_priv and columns_priv Tables. Access Control: How MySQL Uses the Grant Tables. Updating Privileges: When Do Changes Take Effect?
Making Your MySQL Database Secure.
MySQL from the Operating System's Point of View. Passwords. User Privileges. Web Issues.
Getting More Information About Databases.
Getting Information with SHOW. Getting Information About Columns with DESCRIBE. Understanding How Queries Work with EXPLAIN.
Speeding Up Queries with Indexes. General Optimization Tips.
Design Optimization. Permissions. Table Optimization. Using Indexes. Use Default Values. Use Persistent Connections. Other Tips.
Different Table Types. Loading Data from a File. Further Reading. Next.
III. E-COMMERCE AND SECURITY.
Online Brochures. Taking Orders for Goods or Services. Providing Services and Digital Goods. Adding Value to Goods or Services. Cutting Costs.
Risks and Threats.
Crackers. Failing to Attract Sufficient Business. Computer Hardware Failure. Power, Communication, Network, or Shipping Failures. Extensive Competition. Software Errors. Evolving Governmental Policies and Taxes. System Capacity Limits.
Deciding on a Strategy. Next.
13. E-commerce Security Issues.
Exposure of Confidential Data. Loss or Destruction of Data. Modification of Data. Denial of Service. Errors in Software. Repudiation.
Balancing Usability, Performance, Cost, and Security. Creating a Security Policy. Authentication Principles. Using Authentication. Encryption Basics. Private Key Encryption. Public Key Encryption. Digital Signatures. Digital Certificates. Secure Web Servers. Auditing and Logging. Firewalls. Backing Up Data.
Backing Up General Files. Backing Up and Restoring Your MySQL Database.
Physical Security. Next.
14. Implementing Authentication with PHP and MySQL.
Storing Passwords. Encrypting Passwords. Protecting Multiple Pages.
Basic Authentication. Using Basic Authentication in PHP. Using Basic Authentication with Apache's .htaccess Files. Using Basic Authentication with IIS. Using mod_auth_mysql Authentication.
Installing mod_auth_mysql. Did It Work? Using mod_auth_mysql.
Creating Your Own Custom Authentication. Further Reading. Next.
15. Implementing Secure Transactions with PHP and MySQL.
The User's Machine. The Internet. Your System.
Using Secure Sockets Layer (SSL). Screening User Input. Providing Secure Storage. Why Are You Storing Credit Card Numbers? Using Encryption in PHP. Further Reading. Next.
IV. ADVANCED PHP TECHNIQUES.
HTML for File Upload. Writing the PHP to Deal with the File. Common Problems.
Using Directory Functions.
Reading from Directories. Getting Info About the Current Directory. Creating and Deleting Directories.
Interacting with the File System.
Get File Info. Changing File Properties. Creating, Deleting, and Moving Files.
Using Program Execution Functions. Interacting with the Environment: getenv() and putenv(). Further Reading. Next.
17. Using Network and Protocol Functions.
Using FTP to Back Up or Mirror a File. Uploading Files. Avoiding Timeouts. Using Other FTP Functions.
Generic Network Communications with Curl. Further Reading. Next.
18. Managing the Date and Time.
Using the date() Function. Dealing with UNIX Time Stamps. Using the getdate() Function. Validating Dates.
Converting Between PHP and MySQL Date Formats. Date Calculations. Using the Calendar Functions. Further Reading. Next.
19. Generating Images.
JPEG. PNG. WBMP. GIF.
Creating a Canvas Image. Drawing or Printing Text onto the Image. Outputting the Final Graphic. Cleaning Up.
Using Automatically Generated Images in Other Pages. Using Text and Fonts to Create Images.
Setting Up the Base Canvas. Fitting the Text onto the Button. Positioning the Text. Writing the Text onto the Button. Finishing Up.
Drawing Figures and Graphing Data. Other Image Functions. Further Reading. Next.
20. Using Session Control in PHP.
What Is a Cookie? Setting Cookies from PHP. Using Cookies with Sessions. Storing the Session ID.
Implementing Simple Sessions.
Starting a Session. Registering Session Variables. Using Session Variables. Deregistering Variables and Destroying the Session.
Simple Session Example. Configuring Session Control. Implementing Authentication with Session Control. Further Reading. Next.
21. Other Useful Features.
Finding Out What Extensions Are Loaded. Identifying the Script Owner. Finding Out When the Script Was Modified.
Loading Extensions Dynamically. Temporarily Altering the Runtime Environment. Source Highlighting. Next.
V. BUILDING PRACTICAL PHP AND MYSQL PROJECTS.
Coding Standards. Breaking Up Code. Using a Standard Directory Structure. Documenting and Sharing In-House Functions.
Implementing Version Control. Choosing a Development Environment. Documenting Your Projects. Prototyping. Separating Logic and Content. Optimizing Code.
Using Simple Optimizations. Using Zend Products.
Testing. Further Reading. Next.
Syntax Errors. Runtime Errors. Logic Errors.
Variable Debugging Aid. Error Reporting Levels. Altering the Error Reporting Settings. Triggering Your Own Errors. Handling Errors Gracefully. Remote Debugging. Next.
24. Building User Authentication and Personalization.
User Identification and Personalization. Storing Bookmarks. Recommending Bookmarks.
Solution Overview. Implementing the Database. Implementing the Basic Site. Implementing User Authentication.
Registering. Logging In. Logging Out. Changing Passwords. Resetting Forgotten Passwords.
Implementing Bookmark Storage and Retrieval.
Adding Bookmarks. Displaying Bookmarks. Deleting Bookmarks.
Implementing Recommendations. Wrapping Up and Possible Extensions. Next.
25. Building a Shopping Cart.
Building an Online Catalog. Tracking a User's Purchases While She Shops. Payment. Administration Interface.
Solution Overview. Implementing the Database. Implementing the Online Catalog.
Listing Categories. Listing Books in a Category. Showing Book Details.
Implementing the Shopping Cart.
Using the show_cart.php Script. Viewing the Cart. Adding Items to the Cart. Saving the Updated Cart. Printing a Header Bar Summary. Checking Out.
Implementing Payment. Implementing an Administration Interface. Extending the Project. Using an Existing System. Next.
26. Building a Content Management System.
Getting Content into the System. Databases Versus File Storage. Document Structure.
Using Metadata. Formatting the Output. Image Manipulation. Solution Design/Overview. Designing the Database. Implementation.
Front End. Back End. Searching. Editor Screen. Extending the Project.
27. Building a Web-Based Email Service.
Creating a New Account. Modifying an Existing Account. Deleting an Account.
Selecting an Account. Viewing Mailbox Contents. Reading a Mail Message. Viewing Message Headers. Deleting Mail.
Sending a New Message. Replying To or Forwarding Mail.
Extending the Project. Next.
28. Building a Mailing List Manager.
Setting Up a Database of Lists and Subscribers.
File Upload. Sending Mail with Attachments. Solution Overview. Setting Up the Database. Script Architecture. Implementing Login.
Creating a New Account. Logging In.
Implementing User Functions.
Viewing Lists. Viewing List Information. Viewing List Archives. Subscribing and Unsubscribing. Changing Account Settings. Changing Passwords. Logging Out.
Implementing Administrative Functions.
Creating a New List. Uploading a New Newsletter. Handling Multiple File Upload. Previewing the Newsletter. Sending the Message. Extending the Project. Next.
29. Building Web Forums.
Expanding and Collapsing. Displaying the Articles. Using the treenode Class.
Viewing Individual Articles. Adding New Articles. Extensions. Using an Existing System. Next.
30. Generating Personalized Documents in Portable Format (PDF).
Paper. ASCII. HTML. Word Processor Formats. Rich Text Format. PostScript. Portable Document Format.
Question and Answer System. Document Generation Software.
Asking the Questions. Grading the Answers. Generating an RTF Certificate. Generating a PDF Certificate from a Template. Generating a PDF Document Using PDFlib. A Hello World Script for PDFlib. Generating Our Certificate with PDFlib.
Problems with Headers. Extending the Project. Further Reading.
Apache and mod_SSL. httpd.conf File-Snippets. Is SSL Working?
Installing Apache, PHP, and MySQL Under Windows.
Installing MySQL Under Windows. Installing Apache Under Windows. Differences Between Apache for Windows and UNIX. Installing PHP for Windows. Installation Notes for Microsoft IIS. Installation Notes for Microsoft PWS.
Appendix B. Web Resources.