Learning PHP, MySQL, and JavaScript

Book description

If you know HTML, this guide will have you building interactive websites quickly. You'll learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript, regardless of whether you already know how to program. Discover how the powerful combination of PHP and MySQL provides an easy way to build modern websites complete with dynamic data and user interaction. You'll also learn how to add JavaScript to create rich Internet applications and websites.

Learning PHP, MySQL, and JavaScript explains each technology separately, shows you how to combine them, and introduces valuable web programming concepts, including objects, XHTML, cookies, and session management. You'll practice what you've learned with review questions in each chapter, and find a sample social networking platform built with the elements introduced in this book.

This book will help you:

  • Understand PHP essentials and the basics of object-oriented programming
  • Master MySQL, from database structure to complex queries
  • Create web pages with PHP and MySQL by integrating forms and other HTML features
  • Learn about JavaScript, from functions and event handling to accessing the Document Object Model
  • Use libraries and packages, including the Smarty web template system, PEAR program repository, and the Yahoo! User Interface Library
  • Make Ajax calls and turn your website into a highly dynamic environment
  • Upload and manipulate files and images, validate user input, and secure your applications

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. 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. We’d Like to Hear from You
    8. Safari® Books Online
    9. Acknowledgments
  3. 1. Introduction to Dynamic Web Content
    1. HTTP and HTML: Berners-Lee’s Basics
      1. The Request/Response Procedure
    2. The Benefits of PHP, MySQL, and JavaScript
      1. Using PHP
      2. Using MySQL
      3. Using JavaScript
    3. The Apache Web Server
    4. About Open Source
    5. Bringing It All Together
    6. Test Your Knowledge: Questions
  4. 2. Setting Up a Development Server
    1. What Is a WAMP, MAMP, or LAMP?
    2. Installing a WAMP on Windows
      1. Overcoming Installation Problems
      2. Testing the Installation
      3. Alternative WAMPs
    3. Installing a MAMP on Mac OS X
      1. Some Final Tweaking
      2. Other Alternatives
    4. Installing a LAMP on Linux
    5. Working Remotely
      1. Logging In
      2. Using FTP
    6. Using a Program Editor
    7. Using an IDE
    8. Test Your Knowledge: Questions
  5. 3. Introduction to PHP
    1. Incorporating PHP Within HTML
      1. Calling the PHP Parser
    2. This Book’s Examples
    3. The Structure of PHP
      1. Using Comments
      2. Basic Syntax
        1. Semicolons
        2. The $ symbol
      3. Understanding Variables
        1. String variables
        2. Numeric variables
        3. Arrays
        4. Two-dimensional arrays
        5. Variable naming rules
      4. Operators
        1. Arithmetic operators
        2. Assignment operators
        3. Comparison operators
        4. Logical operators
      5. Variable Assignment
        1. Variable incrementing and decrementing
        2. String concatenation
        3. String types
        4. Escaping characters
      6. Multiple-Line Commands
      7. Variable Typing
      8. Constants
        1. Predefined constants
      9. The Difference Between the echo and print Commands
      10. Functions
      11. Variable Scope
        1. Local variables
        2. Global variables
        3. Static variables
        4. Superglobal variables
        5. Superglobals and security
    4. Test Your Knowledge: Questions
  6. 4. Expressions and Control Flow in PHP
    1. Expressions
      1. Literals and Variables
    2. Operators
      1. Operator Precedence
      2. Associativity
      3. Relational Operators
        1. Equality
        2. Comparison operators
        3. Logical operators
    3. Conditionals
      1. The if Statement
      2. The else Statement
      3. The elseif Statement
      4. The switch Statement
        1. Breaking out
        2. Default action
        3. Alternative syntax
      5. The ? Operator
    4. Looping
      1. while Loops
      2. do...while Loops
      3. for Loops
      4. Breaking Out of a Loop
      5. The continue Statement
    5. Implicit and Explicit Casting
    6. PHP Dynamic Linking
      1. Dynamic Linking in Action
    7. Test Your Knowledge: Questions
  7. 5. PHP Functions and Objects
    1. PHP Functions
      1. Defining a Function
      2. Returning a Value
      3. Returning an Array
      4. Passing by Reference
      5. Returning Global Variables
      6. Recap of Variable Scope
    2. Including and Requiring Files
      1. The include Statement
      2. Using include_once
      3. Using require and require_once
    3. PHP Version Compatibility
    4. PHP Objects
      1. Terminology
      2. Declaring a Class
      3. Creating an Object
      4. Accessing Objects
        1. Cloning objects
      5. Constructors
        1. PHP 5 destructors
      6. Writing Methods
        1. Static methods in PHP 5
      7. Declaring Properties
      8. Declaring Constants
      9. Property and Method Scope in PHP 5
        1. Static properties and methods
      10. Inheritance
        1. The parent operator
        2. Subclass constructors
        3. Final methods
    5. Test Your Knowledge: Questions
  8. 6. PHP Arrays
    1. Basic Access
      1. Numerically Indexed Arrays
      2. Associative Arrays
      3. Assignment Using the array Keyword
    2. The foreach...as Loop
    3. Multidimensional Arrays
    4. Using Array Functions
      1. is_array()
      2. count()
      3. sort()
      4. shuffle()
      5. explode()
      6. extract()
      7. compact()
      8. reset()
      9. end()
    5. Test Your Knowledge: Questions
  9. 7. Practical PHP
    1. Using printf
      1. Precision Setting
      2. String Padding
      3. Using sprintf
    2. Date and Time Functions
      1. Date Constants
      2. Using checkdate
    3. File Handling
      1. Checking Whether a File Exists
      2. Creating a File
      3. Reading from Files
      4. Copying Files
      5. Moving a File
      6. Deleting a File
      7. Updating Files
      8. Locking Files for Multiple Accesses
      9. Reading an Entire File
      10. Uploading Files
        1. Using $_FILES
        2. Validation
    4. System Calls
    5. XHTML
      1. The Benefits of XHTML
      2. XHTML Versions
      3. What’s Different?
      4. HTML 4.01 Document Types
      5. XHTML 1.0 Document Types
      6. XHTML Validation
    6. Test Your Knowledge: Questions
  10. 8. Introduction to MySQL
    1. MySQL Basics
    2. Summary of Database Terms
    3. Accessing MySQL via the Command Line
      1. Starting the Command-Line Interface
        1. Windows users
        2. Mac OS X users
        3. Linux users
        4. MySQL on a remote server
      2. Using the Command-Line Interface
        1. The semicolon
        2. Canceling a command
      3. MySQL Commands
        1. Creating a database
        2. Creating users
        3. Creating a table
      4. Data Types
        1. The CHAR data type
        2. The BINARY data type
        3. The TEXT and VARCHAR data types
        4. The BLOB data type
        5. Numeric data types
        6. DATE and TIME
        7. The AUTO_INCREMENT data type
        8. Adding data to a table
        9. Renaming a table
        10. Changing the data type of a column
        11. Adding a new column
        12. Renaming a column
        13. Removing a column
        14. Deleting a table
    4. Indexes
      1. Creating an Index
        1. Using CREATE INDEX
        2. Adding indexes when creating tables
        3. Primary keys
        4. Creating a FULLTEXT index
      2. Querying a MySQL Database
        1. SELECT
        2. SELECT COUNT
        3. SELECT DISTINCT
        4. DELETE
        5. WHERE
        6. LIMIT
        7. MATCH...AGAINST
        8. MATCH...AGAINST...IN BOOLEAN MODE
        9. UPDATE...SET
        10. ORDER BY
        11. GROUP BY
      3. Joining Tables Together
        1. NATURAL JOIN
        2. JOIN...ON
        3. Using AS
      4. Using Logical Operators
    5. MySQL Functions
    6. Accessing MySQL via phpMyAdmin
      1. Windows Users
      2. Mac OS X Users
      3. Linux Users
      4. Using phpMyAdmin
    7. Test Your Knowledge: Questions
  11. 9. Mastering MySQL
    1. Database Design
      1. Primary Keys: The Keys to Relational Databases
    2. Normalization
      1. First Normal Form
      2. Second Normal Form
      3. Third Normal Form
      4. When Not to Use Normalization
    3. Relationships
      1. One-to-One
      2. One-to-Many
      3. Many-to-Many
      4. Databases and Anonymity
    4. Transactions
      1. Transaction Storage Engines
      2. Using BEGIN
      3. Using COMMIT
      4. Using ROLLBACK
    5. Using EXPLAIN
    6. Backing Up and Restoring
      1. Using mysqldump
      2. Creating a Backup File
      3. Restoring from a Backup File
      4. Dumping Data in CSV Format
      5. Planning Your Backups
    7. Test Your Knowledge: Questions
  12. 10. Accessing MySQL Using PHP
    1. Querying a MySQL Database with PHP
      1. The Process
      2. Creating a Login File
      3. Connecting to MySQL
        1. Selecting a database
        2. Building and executing a query
        3. Fetching a result
        4. Fetching a row
        5. Closing a connection
    2. A Practical Example
      1. The $_POST Array
      2. Deleting a Record
      3. Displaying the Form
      4. Querying the Database
      5. Running the Program
    3. Practical MySQL
      1. Creating a Table
      2. Describing a Table
      3. Dropping a Table
      4. Adding Data
      5. Retrieving Data
      6. Updating Data
      7. Deleting Data
      8. Using AUTO_INCREMENT
        1. Using insert IDs
      9. Performing Additional Queries
      10. Preventing SQL Injection
        1. Using placeholders
      11. Preventing HTML Injection
    4. Test Your Knowledge: Questions
  13. 11. Form Handling
    1. Building Forms
    2. Retrieving Submitted Data
      1. register_globals: An Old Solution Hangs On
      2. Default Values
      3. Input Types
      4. Text Boxes
      5. Text Areas
      6. Checkboxes
      7. Radio Buttons
      8. Hidden Fields
      9. Select
      10. Labels
        1. The submit button
      11. Sanitizing Input
    3. An Example Program
    4. Test Your Knowledge: Questions
  14. 12. Templating with Smarty
    1. Why Smarty?
    2. Installation
    3. Creating Scripts
    4. Creating Templates
    5. A Practical Example
    6. Test Your Knowledge: Questions
  15. 13. Cookies, Sessions, and Authentication
    1. Using Cookies in PHP
      1. Setting a Cookie
      2. Accessing a Cookie
      3. Destroying a Cookie
    2. HTTP Authentication
      1. Storing Usernames and Passwords
      2. Salting
    3. Using Sessions
      1. Starting a Session
      2. Ending a Session
        1. Setting a timeout
      3. Session Security
        1. Preventing session hijacking
        2. Preventing session fixation
        3. Forcing cookie-only sessions
        4. Using a shared server
    4. Test Your Knowledge: Questions
  16. 14. Exploring JavaScript
    1. JavaScript and HTML Text
      1. Using Scripts Within a Document Head
      2. Older and Nonstandard Browsers
      3. Including JavaScript Files
      4. Debugging JavaScript Errors
    2. Using Comments
    3. Semicolons
    4. Variables
      1. String Variables
      2. Numeric Variables
      3. Arrays
    5. Operators
      1. Arithmetic Operators
      2. Assignment Operators
      3. Comparison Operators
      4. Logical Operators
      5. Variable Incrementing and Decrementing
      6. String Concatenation
      7. Escaping Characters
    6. Variable Typing
    7. Functions
    8. Global Variables
      1. Local Variables
    9. The Document Object Model
      1. Browser Incompatibilities
        1. Another use for the $ sign
      2. Using the DOM
    10. Test Your Knowledge: Questions
  17. 15. Expressions and Control Flow in JavaScript
    1. Expressions
      1. Literals and Variables
    2. Operators
      1. Operator Precedence
      2. Associativity
      3. Relational Operators
        1. Equality operators
        2. Comparison operators
        3. Logical operators
    3. The with Statement
    4. Using onError
    5. Using try...catch
    6. Conditionals
      1. The if Statement
        1. The else statement
      2. The switch Statement
        1. Breaking out
        2. Default action
      3. The ? Operator
    7. Looping
      1. while Loops
      2. do...while Loops
      3. for Loops
      4. Breaking Out of a Loop
      5. The continue Statement
    8. Explicit Casting
    9. Test Your Knowledge: Questions
  18. 16. JavaScript Functions, Objects, and Arrays
    1. JavaScript Functions
      1. Defining a Function
        1. The arguments array
      2. Returning a Value
      3. Returning an Array
    2. JavaScript Objects
      1. Declaring a Class
      2. Creating an Object
      3. Accessing Objects
      4. The prototype Keyword
        1. Static methods and properties
        2. Extending JavaScript objects
    3. JavaScript Arrays
      1. Numeric Arrays
        1. Assigning element values
        2. Assignment using the array keyword
      2. Associative Arrays
      3. Multidimensional Arrays
      4. Using Array Methods
        1. concat
        2. forEach: For non-IE browsers
        3. forEach: A cross-browser solution
        4. join
        5. push and pop
        6. Using reverse
        7. sort
    4. Test Your Knowledge: Questions
  19. 17. JavaScript and PHP Validation and Error Handling
    1. Validating User Input with JavaScript
      1. The validate.html Document (Part One)
        1. How it works
      2. The validate.html Document (Part Two)
        1. Validating the forename
        2. Validating the surname
        3. Validating the username
        4. Validating the password
        5. Validating the age
        6. Validating the email
        7. Using a separate JavaScript file
    2. Regular Expressions
      1. Matching Through Metacharacters
      2. Fuzzy Character Matching
      3. Grouping Through Parentheses
      4. Character Classes
      5. Indicating a Range
      6. Negation
      7. Some More Complicated Examples
      8. Summary of Metacharacters
      9. General Modifiers
      10. Using Regular Expressions in JavaScript
      11. Using Regular Expressions in PHP
    3. Redisplaying a Form After PHP Validation
    4. Test Your Knowledge: Questions
  20. 18. Using Ajax
    1. What Is Ajax?
    2. Using XMLHttpRequest
      1. Your First Ajax Program
        1. The readyState property
        2. The server half of the Ajax process
      2. Using GET Instead of POST
      3. Sending XML Requests
        1. About XML
        2. Why use XML?
    3. Test Your Knowledge: Questions
  21. 19. Using YUI for Ajax and More
    1. Choosing a Framework
    2. Using YUI
      1. Compressed Versions
      2. Using YUI for Ajax
        1. Including the framework files
        2. The YUI asyncRequest method
        3. An Ajax GET example using YUI
        4. An Ajax XML example using YUI
    3. Other Uses for YUI
      1. A Simple YUI Calendar
    4. Test Your Knowledge: Questions
  22. 20. Bringing It All Together
    1. Designing a Social Networking Site
      1. About Third-Party Add-Ons
    2. On the Website
    3. rnfunctions.php
      1. The Functions
    4. rnheader.php
    5. rnsetup.php
    6. index.php
    7. rnsignup.php
      1. Checking for Username Availability
    8. rnsignup.php (YUI version)
    9. rncheckuser.php
    10. rnlogin.php
    11. rnprofile.php
      1. Adding the “About Me” Text
      2. Adding a Profile Image
      3. Processing the Image
      4. Displaying the Current Profile
    12. rnmembers.php
      1. Viewing a User’s Profile
      2. Adding and Dropping Friends
      3. Listing All Members
    13. rnfriends.php
    14. rnmessages.php
    15. rnlogout.php
  23. A. Solutions to the Chapter Questions
    1. Chapter 1 Answers
    2. Chapter 2 Answers
    3. Chapter 3 Answers
    4. Chapter 4 Answers
    5. Chapter 5 Answers
    6. Chapter 6 Answers
    7. Chapter 7 Answers
    8. Chapter 8 Answers
    9. Chapter 9 Answers
    10. Chapter 10 Answers
    11. Chapter 11 Answers
    12. Chapter 12 Answers
    13. Chapter 13 Answers
    14. Chapter 14 Answers
    15. Chapter 15 Answers
    16. Chapter 16 Answers
    17. Chapter 17 Answers
    18. Chapter 18 Answers
    19. Chapter 19 Answers
  24. B. Online Resources
    1. PHP Resource Sites
    2. MySQL Resource Sites
    3. JavaScript Resource Sites
    4. Ajax Resource Sites
    5. Miscellaneous Resource Sites
    6. O’Reilly Resource Sites
  25. C. MySQL’s FULLTEXT Stopwords
  26. D. MySQL Functions
    1. String Functions
    2. Date Functions
    3. Time Functions
  27. E. Using PEAR and PHPUnit
    1. Installation
      1. Windows
      2. Mac OS
      3. Linux/Unix
    2. Creating a Connect Instance
      1. Querying
      2. Fetching a Row
      3. Closing a Connection
    3. Rewriting Example 10-8 to Use PEAR
    4. Adding Other PEAR Packages
    5. Unit Testing with PHPUnit
  28. Index
  29. About the Author
  30. Colophon
  31. Copyright

Product information

  • Title: Learning PHP, MySQL, and JavaScript
  • Author(s): Robin Nixon
  • Release date: July 2009
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596157135