You are previewing Learning PHP & MySQL, 2nd Edition.
O'Reilly logo
Learning PHP & MySQL, 2nd Edition

Book Description

PHP and MySQL are quickly becoming the de facto standard for rapid development of dynamic, database-driven web sites. This book is perfect for newcomers to programming as well as hobbyists who are intimidated by harder-to-follow books. With concepts explained in plain English, the new edition starts with the basics of the PHP language, and explains how to work with MySQL, the popular open source database. You then learn how to put the two together to generate dynamic content. If you come from a web design or graphics design background and know your way around HTML, Learning PHP & MySQL is the book you've been looking for. The content includes:

  • PHP basics such as strings and arrays, and pattern matching

  • A detailed discussion of the variances in different PHP versions

  • MySQL data fundamentals like tables and statements

  • Information on SQL data access for language

  • A new chapter on XHTML

  • Error handling, security, HTTP authentication, and more

Learning PHP & MySQL explains everything from fundamental concepts to the nuts and bolts of performing specific tasks. As part of O'Reilly's bestselling Learning series, the book is an easy-to-use resource designed specifically for beginners. It's a launching pad for future learning, providing you with a solid foundation for more advanced development.

Table of Contents

  1. Learning PHP & MySQL, 2nd Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Audience
    2. Assumptions This Book Makes
    3. Organization of This Book
    4. Supporting Books
    5. Conventions Used in This Book
    6. Using Code Examples
    7. How to Contact Us
    8. Safari® Books Online
    9. Acknowledgments
  4. 1. Dynamic Content and the Web
    1. HTTP and the Internet
    2. PHP and MySQL's Place in Web Development
      1. Advantages of Using PHP with MySQL
      2. The Value of Open Source
    3. The Components of a PHP Application
      1. PHP
      2. Apache
      3. SQL and Relational Databases
      4. MySQL
      5. Compatibility
    4. Integrating Many Sources of Information
    5. Requesting Data from a Web Page
    6. Questions
  5. 2. Installation
    1. Developing Locally
      1. Bundled or Full Installations
      2. Installing Apache
      3. Installing PHP
        1. Enabling PHP on Mac OS X
      4. Installing MySQL 5.0
        1. Installing the MySQL Connector
        2. Mac OS X MySQL installation
      5. XAMPP
    2. Working Remotely
    3. Questions
  6. 3. Exploring PHP
    1. PHP and HTML Text
      1. Text Output
        1. Separating PHP from HTML
    2. Coding Building Blocks
      1. Variables
        1. Reading a variable's value
        2. Variable types
        3. Variable scope
          1. Global variables
          2. Static variables
          3. Super global variables
      2. Strings
        1. Special characters in strings
        2. Comparing strings
      3. Concatenation
        1. Combining strings
      4. Constants
        1. Predefined constants
      5. Doing Math
        1. Combined assignment
        2. Autoincrement and autodecrement
        3. Preincrement and -decrement
    3. Questions
  7. 4. PHP Decision-Making
    1. Expressions
    2. Operator Concepts
      1. Number of Operands
      2. Types of Operands
        1. Order of precedence
        2. Associativity
      3. Relational Operators
        1. Equality
        2. Comparison operators
        3. Logical operators
    3. Conditionals
      1. The if Statement
        1. The else statement
        2. The elseif statement
      2. The ? Operator
      3. The switch Statement
        1. Breaking out
        2. Defaulting
    4. Looping
      1. while Loops
      2. do ... while Loops
      3. for Loops
      4. Breaking Out of a Loop
      5. continue Statements
    5. Questions
  8. 5. Functions
    1. Calling Functions
    2. Defining Functions
      1. Parameters
      2. Parameter References
      3. Including and Requiring PHP Files
      4. The include Statement
        1. The include_once statement
        2. require and require_once functions
      5. Testing a Function
    3. Object-Oriented Programming
      1. Creating a Class
      2. Creating an Instance
      3. Methods and Constructors
      4. Variable Scope Within Classes
      5. Inheritance
        1. The extends operator
        2. The parent operator
      6. Static Methods and Variables
      7. Variable References
    4. Questions
  9. 6. Arrays
    1. Array Fundamentals
      1. Associative Versus Numeric Indexed Arrays
      2. Creating an Array
        1. Assignment via array identifiers
        2. Assignment using array
        3. Looping through and referencing array values
        4. Adding values to an array
        5. Counting how many elements are in an array
        6. Sorting arrays
      3. Multidimensional Arrays
      4. Extracting Variables from an Array
        1. Using compact to build an array from variables
      5. Array Functions in PHP
    2. Questions
  10. 7. Working with MySQL
    1. MySQL Database
      1. Accessing the Database from the Command Line
        1. Prompts
        2. Commands
    2. Managing the Database
      1. Creating Users
      2. Creating a MySQL Database
    3. Using phpMyAdmin
    4. Database Concepts
    5. Structured Query Language
      1. Creating Tables
      2. Adding Data to a Table
      3. Table Definition Manipulation
        1. Renaming a table
        2. Changing a column's data type
        3. Adding a column
        4. Renaming a column
        5. Removing a column
        6. Deleting an entire table
      4. Querying the Database
        1. Limit results with WHERE
        2. Specifying the order
        3. Joining tables together
        4. Natural joins
        5. Join on
        6. Aliases
      5. Modifying Database Data
      6. Deleting Database Data
      7. Search Functions
      8. Logical Operators
    6. Questions
  11. 8. Database Best Practices
    1. Database Design
      1. Relational Databases
      2. Relationship Types
        1. One-to-one relationships
        2. One-to-many relationships
        3. Many-to-many relationships
      3. Normalization
      4. Forms of Normalization
        1. First Normal Form
        2. Second Normal Form
        3. Third Normal Form
      5. Column Data Types
    2. Backing Up and Restoring Data
      1. Copying Database Files
      2. The mysqldump Command
        1. Backing up
        2. Restoring a MySQL backup
        3. Working with other formats
        4. The mysqlimport command
        5. Backup best practices
    3. Advanced SQL
      1. Indexes
        1. When indexes are used
        2. Where to specify the index
        3. Multicolumn indexes
        4. The EXPLAIN keyword
      2. Selecting with the LEFT JOIN ON Clause
      3. Selecting with GROUP BY
      4. Using Database Functions
        1. String functions
          1. Concatenation
          2. Concatenation with a predefined separator
          3. Calculate a string length
          4. Changing strings to upper- or lowercase
          5. Trimming and padding strings
          6. String location and position
          7. Cutting up strings
          8. Search and replace
        2. Date and time functions
          1. Days, weeks, months, and years
          2. Hours, minutes, and seconds
          3. Dates and times arithmetic
          4. The NOW function
          5. Formatting for display
          6. Unix timestamp conversion
        3. Transactions
    4. Questions
  12. 9. Getting PHP to Talk to MySQL
    1. The Process
      1. Resources
    2. Querying the Database with PHP Functions
      1. Including Database Login Details
      2. Connecting to the Database
        1. Troubleshooting connection errors
      3. Selecting the Database
      4. Building the SQL SELECT Query
      5. Executing the Query
      6. Fetching and Displaying
        1. Fetch types
      7. Closing the Connection
      8. Putting It All Together
    3. Using PEAR
      1. Installing
        1. Unix
        2. Windows
        3. Hosted ISP
      2. Adding Additional Packages
      3. Rewriting the Books Example with PEAR
        1. Creating a connect instance
        2. Connect string
        3. Querying
        4. Fetching
        5. Closing
        6. PEAR error reporting
    4. Questions
  13. 10. Working with Forms
    1. Building a Form
      1. Accessing Submitted Form Values
      2. Default Values
      3. Types of Input
        1. Text boxes
        2. Text areas
        3. Checkboxes
        4. Radio buttons
        5. Hidden
        6. Selects
      4. Working with Multiple Values
      5. Validating Data
        1. Validating checkboxes, radio buttons, and selects
        2. Validating text boxes and text areas
      6. Building a Feet-to-Meters Converter in PHP
      7. Building a Time Zone Conversion Utility in PHP
      8. Querying the Database with Form Data
    2. Templates
      1. Template Engine
      2. Installation
        1. Application level directories
        2. Creating sample scripts
        3. Create a sample template
    3. Questions
  14. 11. Practical PHP
    1. String Functions
      1. Formatting Strings for Display
        1. Using printf
        2. Padding
        3. Specifying precision
        4. Using sprintf
      2. Length
      3. Changing Case
      4. Checking for a String
      5. Using String Position and Substring to Extract a Portion of a String
    2. Date and Time Functions
      1. Display Formats
      2. Arithmetic
      3. Validating Dates
      4. Using mktime to Create a Timestamp
    3. File Manipulation
      1. Functions and Precautions
        1. Permissions
        2. Creating files
        3. Deleting files
        4. Moving files
      2. URL Wrappers
      3. Uploading Files
        1. Accessing the file
        2. Validation
    4. Calling System Calls
    5. Questions
  15. 12. XHTML
    1. Why XHTML?
    2. XHTML and XML Namespaces
    3. XHTML Versions
      1. Document Types
      2. Validation Tools
      3. Common Validation Gotchas
        1. Compatibility with older browsers
    4. Generating XHTML with PHP
    5. Questions
  16. 13. Modifying MySQL Objects and PHP Data
    1. Changing Database Objects from PHP
      1. Creating a Table
      2. Dropping a Table
      3. Errors Happen
    2. Manipulating Table Data
      1. Adding Data
    3. Displaying Results with Embedded Links
    4. Presenting a Form to Add and Process in One File
      1. SQL Injection
      2. Cross-Site Scripting Attacks
    5. Updating Data
    6. Deleting Data
      1. Generating Unique Identifiers
    7. Performing a Subquery
    8. Questions
  17. 14. Cookies, Sessions, and Access Control
    1. Cookies
      1. Setting a Cookie
      2. Accessing a Cookie
      3. Destroying a Cookie
    2. PHP and HTTP Authentication
      1. Storing a Username and Password in a Database
    3. Sessions
      1. Using Sessions
      2. Expanding Our Login Example
      3. Ending a Session
        1. Garbage collection
        2. Setting a session's timeout
      4. Using the Database to Store Sessions
    4. Using Auth_HTTP to Authenticate
    5. Questions
  18. 15. Security
      1. Limit Access to Administrative Pages
      2. Including Files
      3. Storing Passwords in the Database
      4. The Problem with Automatic Global Variables
    1. Session Security
      1. Session Hijacking and Session Fixation
      2. Trusting User Data
      3. Shared Hosting Concerns
      4. Preventing Access to the Database
      5. Blocking Access to the Database for External Hosts
      6. Create Separate Database Users
      7. Cross-Site Scripting
    2. Questions
  19. 16. Validation and Error Handling
    1. Validating User Input with JavaScript
    2. Pattern Matching
        1. Quantifiers
        2. Anchors
        3. Character classes
        4. Executing pattern matches in PHP
        5. preg_match
    3. Redisplaying a Form After PHP Validation Fails
      1. Error Logging
    4. Questions
  20. 17. Sample Application
    1. Configuration File
    2. Page Framework
    3. Database
      1. Sample Data
    4. Displaying a Postings Summary
    5. Displaying a Posting and Its Comments
    6. Adding and Changing Posts
    7. Adding and Changing Comments
    8. Questions
  21. 18. Finishing Your Journey
    1. PHP Coding Standards
      1. Comments
      2. Formatting
        1. Indenting
        2. PHP tags
        3. Templating
        4. Expressions
        5. Function calls
        6. Function definitions
      3. Objects
      4. Naming
      5. Control Structures
        1. Including or requiring PHP files
    2. PEAR
      1. Structured Libraries
        1. Code distribution
        2. PHP Extension Community Library (PECL)
        3. PDO database abstraction
    3. Frameworks
      1. PHP Zend Framework
      2. CakePHP
    4. Ajax
    5. Wikis
    6. Finding Help on the Web
      1. PHP User Groups
    7. Questions
  22. A. Solutions to Chapter Questions
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18
  23. About the Authors
  24. Colophon
  25. Copyright