Table Of ContentN
O
ALL CODE AVAILABLE TO DOWNLOAD TI
DI
E
H
T
X
SI
THE EASIEST WAY TO LEARN PHP
PHP & MYSQL:
PHP & MySQL: Novice to Ninja, 6th Edition is a hands-on guide
YOUR AUTHORS
to learning all the tools, principles, and techniques needed
to build a professional web application using PHP & MySQL. TOM P
NOVICE TO NINJA
Comprehensively updated to cover PHP 7 and modern best BUTLER H
practice, this highly practical and fun book covers everything P
from installing PHP and MySQL through to creating a complete
BY TOM BUTLER
online content management system. Tom is a web developer, a Ph.D
student researching software best
practices, and university lecturer from & KEVIN YANK
the UK with an interest in
• Install PHP & MySQL on Windows, Mac OS X, or Linux programming best practices,
separation of concerns and a “less is
more” approach to code.
• Gain a thorough understanding of PHP syntax
KEVIN
N P
• Master database design principles and SQL
YANK O H
• Write robust, maintainable, best practice code
V P
• Build a working content management system (CMS) I
C &
Before joining Culture Amp in 2015,
• And much more! Kevin taught a generation of web
E
developers during his time at M
SitePoint, starting with the first edition
of the book that you now hold in your T Y
hands. While there, he helped to
launch success stories like 99designs O S
and Flippa. More recently, he quizzed
web developers on HTML, CSS and Q
JavaScript by leading the team N
behind Sit the Test, and has spoken at L
tech conferences around the world. I
N :
J
A
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
& B
Make learning easy and fun YAN UTLE
K R
BUILD YOUR OWN POWERFUL WEB APPLICATIONS
USD $39.95 CAD $52.95
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. Ltd.
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. Ltd., 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. Ltd.
48 Cambridge Street Collingwood
VIC Australia 3066
Web: www.sitepoint.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
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. Visithttp://www.sitepoint.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.
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........................................................6
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.......................................................22
Variables............................................................................................22
Operators...........................................................................................23
Comments.........................................................................................23
Control Structures......................................................................................24
If Statements....................................................................................25
Loops..................................................................................................33
Arrays............................................................................................................45
User Interaction and Forms.......................................................................52
Passing Variables in Links...............................................................53
Passing Variables in Forms.........................................................................62
GET or POST?....................................................................................66
Hiding the Seams........................................................................................66
Table of Contents v
PHP Templates..................................................................................66
Security Concerns............................................................................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.......................................81
Creating a Database...................................................................................87
Structured Query Language......................................................................90
Creating a Table...........................................................................................93
Adding Data.................................................................................................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....................................................................................114
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................................................................139
Thinking Ahead.........................................................................................149
Inserting Data into the Database..........................................................161
Deleting Data from the Database.........................................................173
Mission Accomplished.............................................................................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................................................................193
Simple Relationships...............................................................................199
Many-to-many Relationships................................................................203
One for Many, and Many for One.........................................................207
Chapter 6 Structured PHP Programming.....208
Table of Contents vii
Include Files..............................................................................................209
Including HTML Content.............................................................210
Including PHP Code......................................................................212
Types of Includes...........................................................................215
Custom Functions and Function Libraries...........................................216
Variable Scope...............................................................................220
Breaking Up Your Code Into Reusable Functions...............................228
Using Functions to Replace Queries..........................................235
Updating Jokes..............................................................................238
Editing Jokes on the Website......................................................239
Delete Function.............................................................................243
Select Function.............................................................................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...................................................254
Handling Dates.........................................................................................258
Displaying the Joke Date.............................................................265
viii PHP & MySQL: Novice to Ninja, 6th Edition
Making Your Own Tools..........................................................................268
Generic Functions....................................................................................270
Using These Functions.............................................................................276
Repeated Code Is the Enemy.................................................................280
Creating a Page for Adding and Editing...................................282
Further Polishing......................................................................................287
Moving Forward.......................................................................................293
Chapter 8 Objects and Classes...........................294
Time for Class............................................................................................295
Public vs Private.......................................................................................300
Objects.......................................................................................................301
Class Variables...........................................................................................302
Constructors..............................................................................................309
Type Hinting..............................................................................................313
Private Variables.......................................................................................315
Using the DatabaseTable Class........................................................318
Updating the Controller to Use the Class............................................322
DRY.............................................................................................................326
Creating a Controller Class.....................................................................327
Single Entry Point....................................................................................333
Table of Contents ix
Keeping it DRY..........................................................................................340
Template Variables...................................................................................346
Be Careful With Extract..........................................................................349
Chapter 9 Creating an Extensible
Framework....................................................................353
Search Engines..........................................................................................355
Make Things Generic...............................................................................358
Thinking Ahead: User Registration.......................................................360
Dependencies............................................................................................365
URL Rewriting...........................................................................................373
Tidying Up.................................................................................................378
Make it OOP...................................................................................378
Reusing Code on Different Websites....................................................386
Generic or Project Specific?...................................................................387
Making EntryPoint Generic...................................................389
Autoloading and Namespaces...............................................................396
Case Sensitivity.........................................................................................398
Implement an Autoloader......................................................................399
Redecorating.............................................................................................400
Namespaces...............................................................................................403