You are previewing PHP and MySQL® For Dummies®, 4th Edition.
O'Reilly logo
PHP and MySQL® For Dummies®, 4th Edition

Book Description

Here's what Web designers need to know to create dynamic, database-driven Web sites

To be on the cutting edge, Web sites need to serve up HTML, CSS, and products specific to the needs of different customers using different browsers. An effective e-commerce site gathers information about users and provides information they need to get the desired result.

PHP scripting language with a MySQL back-end database offers an effective way to design sites that meet these requirements. This full updated 4th Edition of PHP & MySQL For Dummies gets you quickly up to speed, even if your experience is limited.

  • Explains the easy way to install and set up PHP and MySQL using XAMPP, so it works the same on Linux, Mac, and Windows

  • Shows you how to secure files on a Web host and how to write secure code

  • Packed with useful and understandable code examples for Web site creators who are not professional programmers

  • Fully updated to ensure your code will be compliant based on PHP 5.3 and MySQL 5.1.31

  • Provides clear, accurate code examples

PHP & MySQL For Dummies, 4th Edition provides what you need to know to create sites that get results.

Table of Contents

  1. Copyright
  2. About the Author
  3. Author's Acknowledgments
  4. Publisher's Acknowledgments
  5. Introduction
    1. About This Book
    2. Conventions Used in This Book
    3. What You're Not To Read
    4. Foolish Assumptions
    5. How This Book Is Organized
      1. Part I: Developing a Web Database Application Using PHP and MySQL
      2. Part II: MySQL Database
      3. Part III: PHP
      4. Part IV: Applications
      5. Part V: The Part of Tens
      6. Part VI: Appendixes
    6. Icons Used in This Book
    7. Where to Go from Here
  6. I. Developing a Web Database Application Using PHP and MySQL
    1. 1. Introduction to PHP and MySQL
      1. 1.1. What Is a Web Database Application?
        1. 1.1.1. The database: Storing data
        2. 1.1.2. The application: Moving data in and out of the database
      2. 1.2. MySQL, My Database
        1. 1.2.1. Advantages of MySQL
        2. 1.2.2. How MySQL works
        3. 1.2.3. Communicating with the MySQL server
      3. 1.3. PHP, a Data Mover
        1. 1.3.1. Advantages of PHP
        2. 1.3.2. How PHP works
      4. 1.4. MySQL and PHP, the Perfect Pair
        1. 1.4.1. Advantages of the relationship
        2. 1.4.2. How MySQL and PHP work together
      5. 1.5. Keeping Up with PHP and MySQL Changes
        1. 1.5.1. PHP versions
        2. 1.5.2. MySQL versions
    2. 2. Setting Up Your Work Environment
      1. 2.1. Anatomy of a Web Site
      2. 2.2. Building a Web Site
      3. 2.3. Deciding Where to Publish Your Web Site
        1. 2.3.1. Using a Web hosting company
        2. 2.3.2. Using a company Web site
        3. 2.3.3. Setting up your own server
      4. 2.4. Deciding Where to Develop Your Web Site
        1. 2.4.1. On your own computer
        2. 2.4.2. On another computer
      5. 2.5. Setting Up Your Web Site
        1. 2.5.1. With a Web hosting company
        2. 2.5.2. On a company Web site
        3. 2.5.3. Information you need
      6. 2.6. Setting Up Your Development Environment
        1. 2.6.1. Your own computer
          1. 2.6.1.1. Installing the Web development software
          2. 2.6.1.2. Writing the files
            1. 2.6.1.2.1. Programming editors
            2. 2.6.1.2.2. Integrated development environment (IDE)
          3. 2.6.1.3. Uploading your files to your Web site
        2. 2.6.2. Web hosting company
          1. 2.6.2.1. Keeping it private
          2. 2.6.2.2. Keeping out the search engines
        3. 2.6.3. A company computer
      7. 2.7. Testing, Testing, 1,2,3
        1. 2.7.1. Understanding PHP/MySQL functions
        2. 2.7.2. Testing PHP
          1. 2.7.2.1. On your local computer
          2. 2.7.2.2. On a Web hosting company
        3. 2.7.3. Testing your local PHP configuration file
        4. 2.7.4. Testing MySQL
    3. 3. Developing a Web Database Application
      1. 3.1. Planning Your Web Database Application
        1. 3.1.1. Identifying what you want from the application
        2. 3.1.2. Taking the user into consideration
        3. 3.1.3. Making the site easy to use
        4. 3.1.4. Leaving room for expansion
        5. 3.1.5. Writing it down
      2. 3.2. Presenting the Two Running Examples in This Book
        1. 3.2.1. Stuff for Sale
        2. 3.2.2. Members Only
      3. 3.3. Designing the Database
        1. 3.3.1. Choosing the data
        2. 3.3.2. Organizing the data
          1. 3.3.2.1. Organizing data in tables
          2. 3.3.2.2. Creating relationships between tables
      4. 3.4. Designing the Sample Databases
        1. 3.4.1. Pet Catalog design process
        2. 3.4.2. Members Only design process
      5. 3.5. Types of Data
        1. 3.5.1. Character data
        2. 3.5.2. Numerical data
        3. 3.5.3. Date and time data
        4. 3.5.4. Enumeration data
        5. 3.5.5. MySQL data type names
        6. 3.5.6. Writing it down
      6. 3.6. Taking a Look at the Sample Database Designs
        1. 3.6.1. Stuff for Sale database tables
        2. 3.6.2. Members Only database tables
      7. 3.7. Developing the Application
        1. 3.7.1. Building the database
        2. 3.7.2. Writing the programs
  7. II. MySQL Database
    1. 4. Building the Database
      1. 4.1. Communicating with MySQL
        1. 4.1.1. Building SQL queries
        2. 4.1.2. Sending SQL queries
          1. 4.1.2.1. Using phpMyAdmin
            1. 4.1.2.1.1. Sending your own SQL queries using the phpMyAdmin query sender
            2. 4.1.2.1.2. Using the phpMyAdmin interface
          2. 4.1.2.2. Using PHP scripts
      2. 4.2. Building a Database
        1. 4.2.1. Creating a new database
          1. 4.2.1.1. On your local computer
          2. 4.2.1.2. On your Web hosting account
        2. 4.2.2. Viewing the databases
        3. 4.2.3. Deleting a database
        4. 4.2.4. Adding tables to a database
          1. 4.2.4.1. Using the phpMyAdmin interface
          2. 4.2.4.2. Writing an SQL query
        5. 4.2.5. Changing the database structure
          1. 4.2.5.1. Using phpMyAdmin
          2. 4.2.5.2. Writing your own SQL query
      3. 4.3. Moving Data Into and Out of the Database
        1. 4.3.1. Adding information
          1. 4.3.1.1. Adding one row at a time with an SQL query
          2. 4.3.1.2. Adding one row at a time with phpMyAdmin
          3. 4.3.1.3. Adding a bunch of data
        2. 4.3.2. Viewing information
        3. 4.3.3. Retrieving information
          1. 4.3.3.1. Retrieving specific information
          2. 4.3.3.2. Retrieving data in a specific order
          3. 4.3.3.3. Retrieving data from a specific source
        4. 4.3.4. Combining information from tables
          1. 4.3.4.1. UNION
          2. 4.3.4.2. Join
        5. 4.3.5. Updating information
        6. 4.3.6. Removing information
    2. 5. Protecting Your Data
      1. 5.1. Controlling Access to Your Data
        1. 5.1.1. Understanding account names and hostnames
        2. 5.1.2. Finding out about passwords
        3. 5.1.3. Taking a look at account privileges
      2. 5.2. Setting Up MySQL Accounts
        1. 5.2.1. Adding accounts
        2. 5.2.2. Allowing access to a database
        3. 5.2.3. Changing privileges
        4. 5.2.4. Adding and changing passwords
        5. 5.2.5. Removing accounts
      3. 5.3. Backing Up Your Data
        1. 5.3.1. Exporting your data with phpMyAdmin
        2. 5.3.2. Viewing the Export file
      4. 5.4. Restoring Your Data
  8. III. PHP
    1. 6. General PHP
      1. 6.1. Adding a PHP Section to an HTML Page
      2. 6.2. Writing PHP Statements
      3. 6.3. Using PHP Variables
        1. 6.3.1. Naming a variable
        2. 6.3.2. Creating and assigning values to variables
        3. 6.3.3. Dealing with notices
      4. 6.4. Using PHP Constants
      5. 6.5. Working with Numbers
      6. 6.6. Working with Character Strings
        1. 6.6.1. Single-quoted strings versus double-quoted strings
        2. 6.6.2. Joining strings
      7. 6.7. Working with Dates and Times
        1. 6.7.1. Setting local time
        2. 6.7.2. Formatting a date
        3. 6.7.3. Storing a timestamp in a variable
        4. 6.7.4. Using dates with MySQL
      8. 6.8. Comparing Values
        1. 6.8.1. Making simple comparisons
        2. 6.8.2. Matching character strings to patterns
      9. 6.9. Joining Comparisons with and/or/xor
      10. 6.10. Adding Comments to Your Program
    2. 7. PHP Building Blocks for Programs
      1. 7.1. Useful Simple Statements
        1. 7.1.1. Using echo statements
        2. 7.1.2. Using assignment statements
        3. 7.1.3. Using increment statements
        4. 7.1.4. Using exit
        5. 7.1.5. Using function calls
      2. 7.2. Using PHP Arrays
        1. 7.2.1. Creating arrays
        2. 7.2.2. Viewing arrays
        3. 7.2.3. Removing values from arrays
        4. 7.2.4. Sorting arrays
        5. 7.2.5. Getting values from arrays
        6. 7.2.6. Walking through an array
          1. 7.2.6.1. Manually walking through an array
          2. 7.2.6.2. Using foreach to walk through an array
        7. 7.2.7. Multidimensional arrays
      3. 7.3. Useful Conditional Statements
        1. 7.3.1. Using if statements
        2. 7.3.2. Using switch statements
      4. 7.4. Using Loops
        1. 7.4.1. Using for loops
        2. 7.4.2. Using while loops
        3. 7.4.3. Using do..while loops
        4. 7.4.4. Infinite loops
        5. 7.4.5. Breaking out of a loop
      5. 7.5. Using Functions
        1. 7.5.1. Using variables in functions
        2. 7.5.2. Passing values between a function and the main program
          1. 7.5.2.1. Passing values to a function
          2. 7.5.2.2. Getting a value from a function
        3. 7.5.3. Using built-in functions
    3. 8. Data In, Data Out
      1. 8.1. PHP and MySQL Functions
      2. 8.2. Making a Connection
        1. 8.2.1. Connecting to the MySQL server
        2. 8.2.2. Selecting the right database
        3. 8.2.3. Sending SQL queries
      3. 8.3. Getting Information from a Database
        1. 8.3.1. Sending a SELECT query
        2. 8.3.2. Getting and using the data
          1. 8.3.2.1. Getting one row of data
          2. 8.3.2.2. Using a loop to get all the rows of data
        3. 8.3.3. Using functions to get data
      4. 8.4. Getting Information from the User
        1. 8.4.1. Using HTML forms
        2. 8.4.2. Making forms dynamic
          1. 8.4.2.1. Displaying dynamic information in form fields
          2. 8.4.2.2. Building selection lists
          3. 8.4.2.3. Building lists of radio buttons
          4. 8.4.2.4. Building lists of check boxes
        3. 8.4.3. Using the information from the form
        4. 8.4.4. Checking the information
          1. 8.4.4.1. Checking for empty fields
          2. 8.4.4.2. Checking the format of the information
        5. 8.4.5. Giving users a choice with multiple submit buttons
      5. 8.5. Putting Information into a Database
        1. 8.5.1. Preparing the data
          1. 8.5.1.1. Putting the data into variables
          2. 8.5.1.2. Using the correct format
          3. 8.5.1.3. Cleaning the data
          4. 8.5.1.4. Escaping the data
        2. 8.5.2. Adding new information
        3. 8.5.3. Updating existing information
      6. 8.6. Getting Information in Files
        1. 8.6.1. Using a form to upload the file
        2. 8.6.2. Processing the uploaded file
        3. 8.6.3. Putting it all together
    4. 9. Moving Information from One Web Page to the Next
      1. 9.1. Moving Your User from One Page to Another
      2. 9.2. Moving Information from Page to Page
        1. 9.2.1. Adding information to the URL
        2. 9.2.2. Storing information via cookies
        3. 9.2.3. Passing information with HTML forms
      3. 9.3. Using PHP Sessions
        1. 9.3.1. Understanding how PHP sessions work
        2. 9.3.2. Opening sessions
        3. 9.3.3. Using PHP session variables
        4. 9.3.4. Sessions without cookies
          1. 9.3.4.1. Sessions with trans-sid turned on
          2. 9.3.4.2. Sessions without trans-sid turned on
        5. 9.3.5. Making sessions private
        6. 9.3.6. Closing PHP sessions
  9. IV. Applications
    1. 10. Putting It All Together
      1. 10.1. Organizing the Application
        1. 10.1.1. Organizing at the application level
        2. 10.1.2. Organizing at the program level
          1. 10.1.2.1. Using include statements
          2. 10.1.2.2. Storing include files
          3. 10.1.2.3. Protecting include files
          4. 10.1.2.4. Using functions
      2. 10.2. Keeping It Private
        1. 10.2.1. Ensure the security of the computer
        2. 10.2.2. Don't let the Web server display filenames
        3. 10.2.3. Hide things
        4. 10.2.4. Don't trust information from users
        5. 10.2.5. Use a secure Web server
      3. 10.3. Completing Your Documentation
    2. 11. Building an Online Catalog
      1. 11.1. Designing the Application
        1. 11.1.1. Showing pets to the customers
        2. 11.1.2. Adding pets to the catalog
      2. 11.2. Building the Database
        1. 11.2.1. Building the Pet table
        2. 11.2.2. Building the PetType table
        3. 11.2.3. Building the Color table
        4. 11.2.4. Adding data to the database
      3. 11.3. Designing the Look and Feel
        1. 11.3.1. Showing pets to the customers
          1. 11.3.1.1. Storefront page
          2. 11.3.1.2. Pet type page
          3. 11.3.1.3. Pets page
        2. 11.3.2. Adding pets to the catalog
          1. 11.3.2.1. Get pet type page
          2. 11.3.2.2. Get pet information page
          3. 11.3.2.3. Feedback page
          4. 11.3.2.4. Get missing information page
      4. 11.4. Writing the Programs
        1. 11.4.1. Showing pets to the customers
          1. 11.4.1.1. Showing the storefront
          2. 11.4.1.2. Showing the pet types
          3. 11.4.1.3. Showing the pets
        2. 11.4.2. Adding pets to the catalog
          1. 11.4.2.1. Writing ChoosePetCat
          2. 11.4.2.2. Writing ChoosePetName
          3. 11.4.2.3. Writing AddPet
    3. 12. Building a Members Only Web Site
      1. 12.1. Designing the Application
      2. 12.2. Building the Database
        1. 12.2.1. Building the Member table
        2. 12.2.2. Building the Login table
        3. 12.2.3. Adding data to the database
      3. 12.3. Designing the Look and Feel
        1. 12.3.1. Storefront page
        2. 12.3.2. Login page
        3. 12.3.3. New Member Welcome page
        4. 12.3.4. Members Only section
      4. 12.4. Writing the Programs
        1. 12.4.1. Writing PetShopFront
        2. 12.4.2. Writing Login
        3. 12.4.3. Writing New_member
        4. 12.4.4. Writing the Members Only section
      5. 12.5. Planning for Growth
  10. V. The Part of Tens
    1. 13. Ten Things You Might Want to Do Using PHP Functions
      1. 13.1. Communicate with MySQL
      2. 13.2. Send E-Mail
      3. 13.3. Use PHP Sessions
      4. 13.4. Stop Your Program
      5. 13.5. Handle Arrays
      6. 13.6. Check for Variables
      7. 13.7. Format Values
      8. 13.8. Compare Strings to Patterns
      9. 13.9. Find Out about Strings
      10. 13.10. Change the Case of Strings
    2. 14. Ten PHP Gotchas
      1. 14.1. Missing Semicolons
      2. 14.2. Not Enough Equal Signs
      3. 14.3. Misspelled Variable Names
      4. 14.4. Missing Dollar Signs
      5. 14.5. Troubling Quotes
      6. 14.6. Invisible Output
      7. 14.7. Numbered Arrays
      8. 14.8. Including PHP Statements
      9. 14.9. Missing Mates
      10. 14.10. Confusing Parentheses and Brackets
  11. VI. Appendixes
    1. A. Installing PHP, MySQL, and Apache from XAMPP
      1. A.1. Installing XAMPP on Windows
      2. A.2. Installing XAMPP on Mac
      3. A.3. Using XAMPP
    2. B. Configuring PHP
      1. B.1. Changing Settings in php.ini
        1. B.1.1. In the general php.ini file
        2. B.1.2. In a php.ini file on your Web site
      2. B.2. Changing Settings with an .htaccess File
      3. B.3. Changing Settings with PHP Statements