Learning PHP, Mysql & Javascript
Total Page:16
File Type:pdf, Size:1020Kb
4th Edition with jQuery Learning PHP, MySQL & JavaScript FOURTH EDITION Build interactive, data-driven websites with the potent combination of Thisisagreatbeginner's “ JavaScript & MySQL PHP, Learning open-source technologies and web standards, even if you have only basic book thatintroduces HTML knowledge. With this popular hands-on guide, you’ll tackle dynamic web programming with the help of today’s core technologies: PHP, MySQL, severalcrucialweb JavaScript, jQuery, CSS, and HTML5. developerlanguages. Explore each technology separately, learn how to use them together, and It'saquick-paced,easy- pick up valuable web programming practices along the way. At the end of to-read,information- the book, you’ll put everything together to build a fully functional social packedbookthatwill networking site, using XAMPP or any development stack you choose. soonhaveyoucreating ■ Learn PHP in-depth, along with the basics of object-oriented dynamicallydrivenweb- programming sites,includingabasic ■ Explore MySQL, from database structure to complex queries socialnetworkingsite.” Learning ■ Use the MySQLi Extension, PHP’s improved MySQL interface —Albert Wiersch developer of CSE HTML Validator ■ Create dynamic PHP web pages that tailor themselves to the user ■ Manage cookies and sessions, and maintain a high level of security P H P, M y S Q L ■ Master the JavaScript language—and enhance it with jQuery ■ Use Ajax calls for background browser/server communication ■ Acquire CSS2 & CSS3 skills for professionally styling your web pages & JavaScript ■ Implement all of the new HTML5 features, including geolocation, audio, video, and the canvas WITH JQUERY, CSS & HTML5 Robin Nixon, an IT journalist who has written hundreds of articles and several books on computing, has developed numerous websites using open source tools, specializing in the technologies featured in this book. Robin has worked with and written about computers since the early 1980s. Nixon WEB DEVELOPMENT Twitter: @oreillymedia facebook.com/oreilly US $49.99 CAN $52.99 ISBN: 978-1-491-91866-1 Robin Nixon 4th Edition with jQuery Learning PHP, MySQL & JavaScript FOURTH EDITION Build interactive, data-driven websites with the potent combination of Thisisagreatbeginner's “ JavaScript & MySQL PHP, Learning open-source technologies and web standards, even if you have only basic book thatintroduces HTML knowledge. With this popular hands-on guide, you’ll tackle dynamic web programming with the help of today’s core technologies: PHP, MySQL, severalcrucialweb JavaScript, jQuery, CSS, and HTML5. developerlanguages. Explore each technology separately, learn how to use them together, and It'saquick-paced,easy- pick up valuable web programming practices along the way. At the end of to-read,information- the book, you’ll put everything together to build a fully functional social packedbookthatwill networking site, using XAMPP or any development stack you choose. soonhaveyoucreating ■ Learn PHP in-depth, along with the basics of object-oriented dynamicallydrivenweb- programming sites,includingabasic ■ Explore MySQL, from database structure to complex queries socialnetworkingsite.” Learning ■ Use the MySQLi Extension, PHP’s improved MySQL interface —Albert Wiersch developer of CSE HTML Validator ■ Create dynamic PHP web pages that tailor themselves to the user ■ Manage cookies and sessions, and maintain a high level of security P H P, M y S Q L ■ Master the JavaScript language—and enhance it with jQuery ■ Use Ajax calls for background browser/server communication ■ Acquire CSS2 & CSS3 skills for professionally styling your web pages & JavaScript ■ Implement all of the new HTML5 features, including geolocation, audio, video, and the canvas WITH JQUERY, CSS & HTML5 Robin Nixon, an IT journalist who has written hundreds of articles and several books on computing, has developed numerous websites using open source tools, specializing in the technologies featured in this book. Robin has worked with and written about computers since the early 1980s. Nixon WEB DEVELOPMENT Twitter: @oreillymedia facebook.com/oreilly US $49.99 CAN $52.99 ISBN: 978-1-491-91866-1 Robin Nixon FOURTH EDITION Learning PHP, MySQL & JavaScript With jQuery, CSS & HTML5 Robin Nixon Learning PHP, MySQL & JavaScript With jQuery, CSS & HTML5 by Robin Nixon Copyright © 2015 Robin Nixon. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editor: Andy Oram Indexer: Ellen Troutman Production Editor: Nicole Shelby Interior Designer: David Futato Copyeditor: Rachel Monaghan Cover Designer: Randy Comer Proofreader: Sharon Wilkey Illustrator: Rebecca Demarest December 2014: Fourth Edition Revision History for the Fourth Edition 2014-11-21: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491918661 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning PHP, MySQL & JavaScript, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-91866-1 [LSI] For Julie Table of Contents Preface. xxiii 1. Introduction to Dynamic Web Content. 1 HTTP and HTML: Berners-Lee’s Basics 2 The Request/Response Procedure 2 The Benefits of PHP, MySQL, JavaScript, CSS, and HTML5 5 Using PHP 6 Using MySQL 7 Using JavaScript 8 Using CSS 9 And Then There’s HTML5 10 The Apache Web Server 11 About Open Source 12 Bringing It All Together 12 Questions 14 2. Setting Up a Development Server. 15 What Is a WAMP, MAMP, or LAMP? 16 Installing XAMPP on Windows 16 Testing the Installation 24 Installing XAMPP on Mac OS X 27 Accessing the Document Root 27 Installing a LAMP on Linux 28 Working Remotely 28 Logging In 28 Using FTP 29 Using a Program Editor 30 Using an IDE 31 v Questions 33 3. Introduction to PHP. 35 Incorporating PHP Within HTML 35 This Book’s Examples 37 The Structure of PHP 38 Using Comments 38 Basic Syntax 39 Variables 40 Operators 45 Variable Assignment 48 Multiple-Line Commands 50 Variable Typing 52 Constants 53 Predefined Constants 54 The Difference Between the echo and print Commands 55 Functions 55 Variable Scope 56 Questions 62 4. Expressions and Control Flow in PHP. 63 Expressions 63 TRUE or FALSE? 63 Literals and Variables 65 Operators 66 Operator Precedence 67 Associativity 69 Relational Operators 70 Conditionals 74 The if Statement 75 The else Statement 76 The elseif Statement 78 The switch Statement 79 The ? Operator 82 Looping 83 while Loops 84 do...while Loops 86 for Loops 86 Breaking Out of a Loop 88 The continue Statement 89 Implicit and Explicit Casting 90 PHP Dynamic Linking 91 vi | Table of Contents Dynamic Linking in Action 92 Questions 93 5. PHP Functions and Objects. 95 PHP Functions 96 Defining a Function 98 Returning a Value 98 Returning an Array 100 Do Not Pass Arguments by Reference 100 Returning Global Variables 102 Recap of Variable Scope 103 Including and Requiring Files 103 The include Statement 104 Using include_once 104 Using require and require_once 105 PHP Version Compatibility 105 PHP Objects 106 Terminology 106 Declaring a Class 107 Creating an Object 108 Accessing Objects 109 Cloning Objects 110 Constructors 111 PHP 5 Destructors 112 Writing Methods 112 Static Methods in PHP 5 113 Declaring Properties 114 Declaring Constants 115 Property and Method Scope in PHP 5 115 Static Properties and Methods 116 Inheritance 118 Questions 121 6. PHP Arrays. 123 Basic Access 123 Numerically Indexed Arrays 123 Associative Arrays 125 Assignment Using the array Keyword 126 The foreach...as Loop 127 Multidimensional Arrays 129 Using Array Functions 132 is_array 132 Table of Contents | vii count 132 sort 133 shuffle 133 explode 133 extract 134 compact 135 reset 136 end 136 Questions 137 7. Practical PHP. 139 Using printf 139 Precision Setting 140 String Padding 142 Using sprintf 143 Date and Time Functions 143 Date Constants 146 Using checkdate 146 File Handling 147 Checking Whether a File Exists 147 Creating a File 147 Reading from Files 149 Copying Files 150 Moving a File 150 Deleting a File 151 Updating Files 151 Locking Files for Multiple Accesses 152 Reading an Entire File 154 Uploading Files 155 System Calls 160 XHTML or HTML5? 162 Questions 162 8. Introduction to MySQL. 165 MySQL Basics 165 Summary of Database Terms 166 Accessing MySQL via the Command Line 166 Starting the Command-Line Interface 167 Using the Command-Line Interface 171 MySQL Commands 172 Data Types 177 Indexes 186 viii | Table of Contents Creating an Index 186 Querying a MySQL Database 192 Joining Tables Together 202 Using Logical Operators 204 MySQL Functions 204 Accessing MySQL via phpMyAdmin 205 Questions 206 9. Mastering MySQL. 209 Database Design 209 Primary Keys: The Keys to Relational Databases 210 Normalization 211 First Normal Form 212 Second Normal Form 214 Third Normal Form 217 When Not to Use Normalization 219 Relationships 219 One-to-One 219 One-to-Many 220 Many-to-Many 221 Databases and Anonymity 222 Transactions 223 Transaction Storage Engines 223 Using BEGIN 224 Using COMMIT 225 Using ROLLBACK 225 Using EXPLAIN 226 Backing Up and Restoring 227 Using mysqldump 227 Creating a Backup File 229 Restoring from a Backup File 231 Dumping Data in CSV Format 231 Planning Your Backups 232 Questions 232 10.