ALL CODE AVAILABLE TO DOWNLOAD THE EASIEST WAY TO LEARN PHP PHP & MySQL: Novice to Ninja, 6th Edition is a hands-on guide to learning all the tools, principles, and techniques needed to build a professional web application using PHP & MySQL. YOUR AUTHORS PHP & MYSQL: NOVICE TO NINJA PHP TOM Comprehensively updated to cover PHP 7 and modern best BUTLER practice, this highly practical and fun book covers everything from installing PHP and MySQL through to creating a complete online content management system. Tom is a web developer, a Ph.D student researching software best BY TOM BUTLER practices, and university lecturer from the UK with an interest in & KEVIN YANK programming best practices, • Install PHP & MySQL on Windows, Mac OS X, or Linux separation of concerns and a “less is more” approach to code. • Gain a thorough understanding of PHP syntax KEVIN NOVICE TO NINJA PHP & MYSQL: • Master database design principles and SQL YANK • Write robust, maintainable, best practice code • Build a working content management system (CMS) Before joining Culture Amp in 2015, • And much more! Kevin taught a generation of web developers during his time at SitePoint, starting with the first edition of the book that you now hold in your hands.
While there, he helped to launch success stories like 99designs and Flippa. More recently, he quizzed web developers on HTML, CSS and JavaScript by leading the team behind Sit the Test, and has spoken at tech conferences around the world. WEB DEVELOPMENT PRINT ISBN: 978-0-9943469-8-8 SITEPOINT BOOKS Advocate best practice techniques Lead you through practical examples Provide working code for your website BUTLER & YANK Make learning easy and fun USD $39.95 BUILD YOUR OWN POWERFUL WEB APPLICATIONS Visit us on the Web at sitepoint.com or for sales and support email books@sitepoint.com SIXTH EDITION i PHP & MySQL: Novice to Ninja, 6th Edition by Tom Butler and Kevin Yank Copyright © 2017 SitePoint Pty. Product Manager: Simon Mackie Technical Editor: Bruno Škvorc English Editor: Ralph Mason Cover Designer: Alex Walker Notice of Rights All rights reserved.
No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein.
Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. 48 Cambridge Street Collingwood VIC Australia 3066 Web: www.com Email: books@sitepoint.com ISBN 978-0-9943469-8-8 (print) ISBN 978-0-9953827-8-7 (ebook) Printed and bound in the United States of America www.ir ii PHP & MySQL: Novice to Ninja, 6th Edition About Tom Butler Tom is a web developer, a Ph.D student researching software best practices, and university lecturer from the UK with an interest in programming best practices, separation of concerns and a “less is more” approach to code. About Kevin Yank Before joining Culture Amp in 2015, Kevin taught a generation of web developers during his time at SitePoint, starting with the first edition of the book that you now hold in your hands.
While there, he helped to launch success stories like 99designs and Flippa. More recently, he quizzed web developers on HTML, CSS and JavaScript by leading the team behind Sit the Test, and has spoken at tech conferences around the world. On weekends he performs improvised theatre with Impro Melbourne, which is a lot more like building websites than you might expect. About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals.
Visit http://www.com/ to access our blogs, books, newsletters, articles, and community forums. You’ll find a stack of information on JavaScript, PHP, Ruby, mobile development, design, and more.ir Table of Contents iii Table of Contents Preface .xv Who Should Read This Book .xvi Programming Has Changed.xvi It Takes 10,000 Hours to Become an Expert .xvii Resist the Temptation to Skip Ahead .xviii The Concorde Fallacy .xviii You’re Not Learning PHP .xix Getting Braces and Semicolons in the Right Place Is the Easy Part. xx You Won’t Get Anything Done by Planning. xx Conventions Used.
xxiii Supplementary Materials.xxiv Chapter 1 Installation .1 Your Own Web Server. 3 Server Setup 1: Manually Installing All the Software Components. 4 Server Setup 2: Pre-packaged Installations. 5 Server Setup 3: Virtual Servers.ir iv PHP & MySQL: Novice to Ninja, 6th Edition Your Own Virtual Server.
8 Installation on Windows. 8 Installation on macOS. 8 Installation on Linux. 9 Getting Started with Vagrant.
9 Chapter 2 Introducing PHP.15 Basic Syntax and Statements.18 Variables, Operators, and Comments .45 User Interaction and Forms .52 Passing Variables in Links .53 Passing Variables in Forms .62 GET or POST? .66 Hiding the Seams .ir Table of Contents v PHP Templates .69 Many Templates, One Controller .73 Bring on the Database .77 Chapter 3 Introducing MySQL.79 An Introduction to Databases .80 Using MySQL Workbench to Run SQL Queries.87 Structured Query Language .98 A Word of Warning. 104 Viewing Stored Data. 105 Modifying Stored Data.110 Deleting Stored Data. 111 Let PHP Do the Typing .112 Chapter 4 Publishing MySQL Data on the Web .ir vi PHP & MySQL: Novice to Ninja, 6th Edition The Big Picture.114 Creating a MySQL User Account .116 Connecting to MySQL with PHP.
120 A Crash Course in Object-oriented Programming. 126 Configuring the Connection. 129 Sending SQL Queries with PHP. 134 Handling SELECT Result Sets.
149 Inserting Data into the Database. 161 Deleting Data from the Database. 182 Chapter 5 Relational Database Design .183 Giving Credit Where Credit Is Due. 184 Rule of Thumb: Keep Entities Separate.
187 SELECT with Multiple Tables. 199 Many-to-many Relationships. 203 One for Many, and Many for One. 207 Chapter 6 Structured PHP Programming .ir Table of Contents vii Include Files.
209 Including HTML Content. 210 Including PHP Code. 212 Types of Includes. 215 Custom Functions and Function Libraries.
220 Breaking Up Your Code Into Reusable Functions. 228 Using Functions to Replace Queries. 238 Editing Jokes on the Website. 244 The Best Way.
246 Chapter 7 Improving the Insert and Update Functions .247 Improving the Update Function. 247 Stripping the Trailing Comma. 252 Improving the Insert Function. 258 Displaying the Joke Date .ir viii PHP & MySQL: Novice to Ninja, 6th Edition Making Your Own Tools.
270 Using These Functions. 276 Repeated Code Is the Enemy. 280 Creating a Page for Adding and Editing. 293 Chapter 8 Objects and Classes .294 Time for Class.
295 Public vs Private. 315 Using the DatabaseTable Class. 318 Updating the Controller to Use the Class. 326 Creating a Controller Class.
327 Single Entry Point .ir Table of Contents ix Keeping it DRY. 346 Be Careful With Extract. 349 Chapter 9 Creating an Extensible Framework. 355 Make Things Generic.
358 Thinking Ahead: User Registration. 378 Make it OOP. 378 Reusing Code on Different Websites. 386 Generic or Project Specific?.
387 Making EntryPoint Generic. 389 Autoloading and Namespaces. 398 Implement an Autoloader .ir x PHP & MySQL: Novice to Ninja, 6th Edition Autoloading with PSR-4. 406 A Note on Composer.
409 And the REST. 410 Enforcing Dependency Structure with Interfaces. 419 Your Own Framework. 422 Chapter 10 Allowing Users to Register Accounts .423 Validating Email Addresses.
433 Preventing the Same Person from Registering Twice. 434 Securely Storing Passwords. 442 Chapter 11 Cookies, Sessions, and Access Control. 454 Counting Visits with Sessions .ir Table of Contents xi Protected Pages.
467 Interfaces and Return Types. 472 Making Use of the Authentication Class. 474 Login Error Message. 476 Creating a Login Form.
483 Assigning Added Jokes to the Logged-in User. 490 The Sky’s the Limit. 494 Chapter 12 MySQL Administration.496 Backing Up MySQL Databases. 497 Database Backups Using MySQL Workbench.
498 Database Backups Using mysqlpump. 500 Incremental Backups Using Binary Logs. 502 MySQL Access Control Tips. 506 Host Name Issues .ir xii PHP & MySQL: Novice to Ninja, 6th Edition Foreign Keys.
518 Better Safe than Sorry .521 Object Relational Mappers. 528 Methods in Entity Classes. 529 Using Entity Classes from the DatabaseTable Class. 540 Using the Joke Class.
545 Simplifying the List Controller Action. 558 Assigning Jokes to Categories. 560 Assigning Categories to Jokes. 574 Displaying Jokes by Category.ir Table of Contents xiii Creating a Form to Assign Permissions.
595 Edit Author Permissions. 600 A Crash Course in Binary. 602 Be Bit-Wise. 603 Back to PHP.
604 Storing Bitwise Permissions in the Database. 607 Join Table or Bitwise. 610 Editing Others’ Jokes. 614 Chapter 14 Content Formatting with Regular Expressions.
617 String Replacement with Regular Expressions .ir xiv PHP & MySQL: Novice to Ninja, 6th Edition Putting It All Together. 633 Sorting, Limiting and Offsets. 639 Pagination with LIMIT and OFFSET. 645 Pagination in Categories.
653 Achievement Unlocked: Ninja. 658 Appendix A Using The Sample Code Repository. 661 Appendix B Linux Troubleshooting .ir Preface xv Preface It was 1998, I was twelve, and my parents had just bought the family our first modern PC. It wasn’t long before I had figured out how to change the code for one of my favorite first-person shooter games—little things like making the rocket launcher fire a hundred rockets a second instead of one, then having it fire a hundred rockets in every direction … and promptly crashing the game.
I was hooked, and I’ve been programming ever since. The game was multiplayer. Other people had also discovered how to change the code, and the arms race quickly escalated. Someone would fire a hundred rockets at me.
I’d have a script ready that would instantly build a wall right in front of me to block them all. My opponent would spawn a dozen land mines underneath me. I’d turn off the gravity, then jump, soaring away from the impending explosion. Everyone could fly.
It got to the point where it was no longer fun. You’d enter a game and someone had written a script to teleport you to the other side of the map, kill you instantly and force you to respawn, repeating the process a dozen times a second. They’d freeze your controls too, of course. We discovered ways to block all this, but by the end it was a stalemate.
Whoever managed to enter the game first could take complete control of it, and no matter how good your scripts were, there was nothing you could do. It was fun while it lasted. That’s how I learned the basics of coding, and that the only limit is your own imagination and creativity. During that time, I’d also taught myself HTML, and had my own website where I shared some of my game hacking techniques and scripts.
No, the website is not still up. Yes, it was terrible, full of bad grammar and cheesy animations (which was the style at the time, I promise!).