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
Table of Contents iiiYour 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
iv PHP & MySQL: Novice to Ninja, 6th EditionPHP 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
Table of Contents vThe 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
vi PHP & MySQL: Novice to Ninja, 6th EditionInclude 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
Table of Contents viiMaking 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
viii PHP & MySQL: Novice to Ninja, 6th EditionKeeping 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
Table of Contents ixAutoloading 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....................................................................439
Registration Complete.............................................................................442
Chapter 11 Cookies, Sessions, and Access
Control.............................................................................446
Cookies.......................................................................................................447
PHP Sessions .............................................................................................454
Counting Visits with Sessions.....................................................457
Access Control ..........................................................................................458
Logging In ......................................................................................458
x PHP & MySQL: Novice to Ninja, 6th EditionProtected Pages.............................................................................467
Interfaces and Return Types .......................................................472
Making Use of the Authentication Class..................................474
Login Error Message.....................................................................476
Creating a Login Form.............................................................................478
Logging Out ..............................................................................................483
Assigning Added Jokes to the Logged-in User ........................485
User Permissions ...........................................................................488
Mission Accomplished?................................................................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.......................................................................... 507
Locked Out? ...................................................................................509
Indexes....................................................................................................... 510
Multicolumn Indexes ................................................................... 515
Table of Contents xiForeign Keys.............................................................................................. 518
Better Safe than Sorry ............................................................................520
Chapter 13 Relationships......................................521
Object Relational Mappers .....................................................................526
Public Properties...........................................................................528
Methods in Entity Classes............................................................529
Using Entity Classes from the DatabaseTable Class................532
Joke Objects...................................................................................540
Using the Joke Class.....................................................................544
References......................................................................................545
Simplifying the List Controller Action ......................................546
Tidying Up .................................................................................................378
Caching...........................................................................................550
Joke Categories.........................................................................................553
List Page .........................................................................................558
Assigning Jokes to Categories................................................................560
Assigning Categories to Jokes ....................................................574
Displaying Jokes by Category.................................................................577
Editing Jokes .............................................................................................583
User Roles..................................................................................................587
xii PHP & MySQL: Novice to Ninja, 6th EditionCreating a Form to Assign Permissions ................................................594
Author List .....................................................................................595
Edit Author Permissions...............................................................596
Setting Permissions ......................................................................599
A Different Approach...................................................................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 ....................................................................609
Cleaning Up ................................................................................... 610
Editing Others’ Jokes.....................................................................611
Phew!.......................................................................................................... 614
Chapter 14 Content Formatting with Regular
Expressions ...................................................................616
Regular Expressions ................................................................................. 617
String Replacement with Regular Expressions ................................... 621
Emphasized Text............................................................................622
Paragraphs......................................................................................628
Hyperlinks ...................................................................................... 631
Table of Contents xiiiPutting It All Together ............................................................................633
Sorting, Limiting and Offsets.................................................................639
Sorting............................................................................................639
Pagination with LIMIT and OFFSET .......................................645
Pagination in Categories .............................................................653
Achievement Unlocked: Ninja ...............................................................657
What Next?....................................................................................658
Appendix A Using The Sample Code
Repository......................................................................660
Some Caveats............................................................................................ 661
Appendix B Linux Troubleshooting.................662
· · · · · · (
收起)