class="bi x0 y0 w0 h1"
PHP,
MySQL
®
,
Javascript
®
& HTML5
ALL-IN-ONE
by Steve Suehring and Janet Valade
PHP,
MySQL
®
,
Javascript
®
& HTML5
ALL-IN-ONE
PHP, MySQL
®
, JavaScript
®
& HTML5 All-in-One For Dummies
®
Published by
John Wiley & Sons, Inc.
111 River Street
OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR-
THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR-
MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE.
FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE
CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material
included with standard print versions of this book may not be included in e-books or in print-on-demand.
If this book refers to media such as a CD or DVD that is not included in the version you purchased, you
may download this material at . For more information about Wiley
products, visit www.wiley.com.
Library of Congress Control Number: 2013932114
ISBN 978-1-118-21370-4 (pbk); ISBN 978-1-118-22874-6 (ebk); ISBN 978-1-118-23134-0 (ebk);
ISBN 978-1-118-26617-5 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
About the Authors
Steve Suehring is the author of several technology books. Steve has written
web applications, big and small, for a variety of organizations and in a variety
of programming languages. Steve’s expertise is in nding creative solutions
to complex problems and complex solutions to simple problems.
Janet Valade is the author of PHP & MySQL For Dummies, which is in its third
edition. She has also written PHP & MySQL Everyday Apps For Dummies and
PHP & MySQL: Your visual blueprint for creating dynamic, database-driven Web
sites. In addition, Janet is the author of Spring into Linux and a coauthor of
Mastering Visually Dreamweaver CS3 and Flash CS3 Professional.
Janet has 20 years of experience in the computing eld. Most recently, she
worked as a Web designer and programmer in an engineering rm for four
this joint project.
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments at .
For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974,
outside the U.S. at 317-572-3993, or fax 317-572-4002.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and
Vertical Websites
Project Editor: Heidi Unger
(Previous Edition: Jean Nelson)
Acquisitions Editor: Kyle Looper
Copy Editor: Debbye Butler
Technical Editor: Peter Veverka
Editorial Manager: Kevin Kirschner
Vertical Websites: Richard Graves
Editorial Assistant: Annie Sullivan
Sr. Editorial Assistant: Cherie Case
Cover Photo: © pagadesign/iStockphoto
Composition Services
Project Coordinator: Patrick Redmond
Layout and Graphics: Jennifer Creasey
Proofreaders: Jessica Kramer, Sossity R. Smith
Indexer: BIM Indexing & Proofreading Services
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Kathleen Nebenhaus, Vice President and Executive Publisher
Chapter 2: Administering MySQL 457
Chapter 3: Designing and Building a Database 475
Chapter 4: Using the Database 497
Chapter 5: Communicating with the Database from PHP Scripts 515
Book VI: Web Applications 529
Chapter 1: Improving Your PHP Programs 531
Chapter 2: Creating and Using a Web Service 541
Chapter 3: Validating Web Forms with JavaScript and PHP 555
Chapter 4: Building a Members-Only Website 587
Book VII: PHP and Templates 633
Chapter 1: Conguring PHP 635
Chapter 2: Building a Templating System 641
Index 655
Table of Contents
Introduction 1
About This Book 1
Foolish Assumptions 1
How This Book Is Organized 2
Book I: Getting Started with PHP and MySQL 2
Book II: HTML and CSS 2
Book III: JavaScript 2
Book IV:PHP 2
Book V: MySQL 2
Book VI: Web Applications 2
Book VII: PHP and Templates 2
Companion Website 3
Icons Used in This Book 3
Where to Go from Here 3
Book I: Getting Started with PHP & MySQL 5
Chapter 1: Understanding the Languages of the Web 7
Verifying a downloaded le 24
Installing Apache 25
Installing Apache on Windows 25
Installing Apache on a Mac 27
Installing Apache from source code on Linux and Mac 27
Starting and Stopping Apache 28
Starting and stopping Apache on Windows 28
Starting Apache on Linux, Unix, and Mac 29
Restarting Apache on Linux, Unix, and Mac 30
Stopping Apache on Linux, Unix, and Mac 30
Getting Information from Apache 31
Getting Apache information on Windows 31
Getting Apache information on Linux, Unix, and Mac 31
Conguring Apache 32
Changing settings 32
Changing the location of your Document Root 33
Changing the port number 33
Chapter 3: Installing PHP 35
Checking the PHP Installation 36
Obtaining PHP 36
Downloading from the PHP website 37
Obtaining PHP for Windows 37
Obtaining PHP for Linux 37
Obtaining PHP for the Mac OS 38
Obtaining all-in-one installation kits 38
Verifying a downloaded le 39
Installing PHP 40
Installing on Unix and Linux 40
Installing on Mac OS X 42
Installation options for Unix, Linux, and Mac 44
Controlling the server on Windows 66
Controlling the MySQL server on Linux and Mac 67
Testing MySQL 68
Troubleshooting MySQL 69
Displays error message: Access denied 69
Displays error message: Client does not support
authentication protocol 69
Displays error message: Can’t connect to . . . 70
MySQL error log 70
The MySQL Administration Program 70
Activating MySQL Support 71
Activating MySQL support on Windows 71
Activating MySQL support on Linux and the Mac OS 71
Checking MySQL support 72
Troubleshooting PHP and MySQL 73
Displays error message: Undened function 73
MySQL functions not activated (Windows) 74
Chapter 5: Setting Up Your Web Development Environment
with the XAMPP Package 75
Obtaining XAMPP 75
Installing XAMPP 76
PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
xiv
Using the XAMPP Control Panel 78
Testing Your Development Environment
79
Opening the XAMPP web page
80
Testing phpMyAdmin
81
Putting text on a page 95
Creating your rst page
97
Choosing block-level or inline elements
98
Inserting line breaks and spaces 99
Making your document easier to maintain
101
Adding lists and tables
102
Practicing Creating a Table
105
Including Links and Images on Your Web Page
108
Adding links
108
Adding images
113
Writing Valid HTML
116
Validating Your HTML
117
Chapter 2: Adding Style with CSS 121
Discovering What CSS Can and Can’t Do for Your Web Page 121
What is CSS?
121
Why use CSS? 122
Limitations of CSS
122
Connecting CSS to a Page
Chapter 3: Creating and Styling Web Forms 169
Using Web Forms to Get Information 169
Understanding web forms 170
Looking at form elements 170
Creating a Form 172
All about the form element 172
Adding a text input 173
Adding a drop-down box 174
Creating check boxes 176
Using radio buttons 178
Submitting and clearing the form 179
Using CSS to Align Form Fields 180
Book III: JavaScript 185
Chapter 1: Understanding JavaScript Basics 187
Viewing the World of JavaScript 187
JavaScript isn’t Java 187
Knowing what JavaScript can do 188
Examining the Ways to Add JavaScript to a Page 188
Adding the JavaScript tag 189
Adding JavaScript to a page’s HTML 189
Using external JavaScript 190
PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
xvi
Chapter 2: Building a JavaScript Program 191
Getting Started with JavaScript Programming 191
Sending an alert to the screen 191
Adding comments 193
Holding data for later in variables 193
Holding multiple values in an array 195
Creating strings to keep track of words 195
jQuery selectors up close 226
Filtering 227
Working with HTML Using jQuery 227
Adding HTML to a page 227
Changing elements 230
Changing Attributes and Styles 232
Reading attributes 233
Writing attributes 234
Changing CSS 237
Table of Contents
xvii
Chapter 4: Reacting to Events with JavaScript and jQuery 241
Understanding Events 241
Working with Forms 242
Adding a Submit Handler 242
Checking for blank elds 246
Monitoring Mouse Events 247
Capturing mouse clicks 247
Watching mouse movements 251
Reacting to Keyboard Events 254
Counting characters 254
Preventing character input 257
Chapter 5: Troubleshooting JavaScript Programs 261
Employing Basic JavaScript Troubleshooting Techniques 261
Adding alerts 262
Using comments in JavaScript 262
Identifying JavaScript Problems with Firebug 264
Installing Firebug 264
Using Firebug 266
Book IV: PHP 269
Setting local time 307
Formatting a date 308
Storing a timestamp in a variable 309
Understanding PHP Error Messages 310
Types of PHP error messages 310
Displaying error messages 313
Logging error messages 315
Adding Comments to Your PHP Script 316
Chapter 2: Building PHP Scripts 319
Setting Up Conditions 320
Comparing values 320
Checking variable content 322
Pattern matching with regular expressions 323
Joining multiple comparisons 327
Using Conditional Statements 329
Using if statements 330
Using switch statements 333
Repeating Actions with Loops 335
Using for loops 335
Using while loops 339
Using do while loops 341
Avoiding innite loops 343
Breaking out of a loop 344
Using Functions 346
Creating a function 347
Using variables in functions 347
Passing values to a function 349
Returning a value from a function 354
Using built-in functions 356
Organizing Scripts 357
Using other delimiters 391
Using SQLite 394
Chapter 4: Object-Oriented Programming 397
Introducing Object-Oriented Programming 397
Objects and classes 398
Properties 399
Methods 399
Inheritance 400
Developing an Object-Oriented Script 400
Choosing objects 401
Selecting properties and methods for each object 401
Creating and using an object 402
Dening a Class 402
Writing a class statement 403
Setting properties 403
Accessing properties using $this 404
Adding methods 405
Understanding public and private properties and methods 407
Writing the constructor 409
Putting it all together 410
Using a Class in a Script 413
Using Abstract Methods in Abstract Classes and Interfaces 415
Using an abstract class 415
Using interfaces 417
Preventing Changes to a Class or Method 418
Handling Errors with Exceptions 419
Copying Objects 420
PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies
xx
Comparing Objects 421
Understanding Other Session Options 446
Book V: MySQL 447
Chapter 1: Introducing MySQL 449
Examining How MySQL Works 449
Understanding Database Structure 450
Communicating with MySQL 450
Building SQL queries 451
Sending SQL queries 452
Using the mysql client 453
Protecting Your MySQL Databases 454
Table of Contents
xxi
Chapter 2: Administering MySQL 457
Understanding the Administrator Responsibilities 457
Default Access to Your Data 458
Controlling Access to Your Data 459
Account names and hostnames 460
Passwords 461
Account privileges 461
Setting Up MySQL Accounts 462
Identifying what accounts currently exist 464
Adding accounts 465
Adding and changing passwords 465
Changing privileges 466
Removing accounts 467
Backing Up Your Database 468
Backing up on Windows 469
Backing up on Linux, Unix, and Mac 469
Restoring Your Data 471
Upgrading MySQL 473
PHP Functions That Communicate with MySQL 516
Communicating with MySQL 516
Connecting to the MySQL server 517
Sending an SQL statement 519
Sending multiple queries 520
Selecting a Database 521
Handling MySQL Errors 522
Using Other Helpful mysqli Functions 523
Counting the number of rows returned by a query 523
Determining the last auto entry 524
Counting affected rows 525
Escaping characters 525
Converting mysqli Functions to mysql Functions 526
Book VI: Web Applications 529
Chapter 1: Improving Your PHP Programs 531
Automatically Including Helper Functions 531
Using auto_prepend_le 531
Starting sessions with a prepended le 532
Using classes for efciency 534
Reusing Code 535
Using functions 536
Using object-oriented programming 539
Chapter 2: Creating and Using a Web Service 541
Understanding Web Services 541
Returning Data from a Web Service 542
Returning the date 542
Returning web service data from a database 545
Accepting Input to a Web Service 548
Querying with input data 548
Returning XML results 550
Creating a User Object 607
Building the User class 607
Building the login-process PHP le 610
Adding Authenticated Pages 612
Building a protected page 612
Building a log out page 614
Adding E-mail Functionality 618
Building the password reset database 619
Building the password recovery page 619
Building the process les 625
Building the class methods 628
Book VII: PHP and Templates 633
Chapter 1: Conguring PHP 635
Understanding the php.ini 635
Working with the php.ini 635
Making changes outside of the php.ini 636
Understanding Common Conguration Changes 636
Changing session timeout 636
Changing other session parameters 637
Disabling functions and classes 637
Changing error display 639
Changing resource limits 639