Thứ Hai, 10 tháng 3, 2014

Build your own database driven web site using php

About the Author
As Technical Director for SitePoint, Kevin Yank keeps abreast of all that is new and exciting
in web technology. Best known for the book you are reading right now, he also co-authored
Simply JavaScript (http://www.sitepoint.com/books/javascript1/) with Cameron Adams and
Everything You Know About CSS Is Wrong! (http://www.sitepoint.com/books/csswrong1/ )
with Rachel Andrew. In addition, Kevin hosts the SitePoint Podcast
(http://www.sitepoint.com/podcast/) and writes the SitePoint Tech Times, a free email
newsletter that goes out to over 240,000 subscribers worldwide.
Kevin lives in Melbourne, Australia and enjoys speaking at conferences, as well as visiting
friends and family in Canada. He’s also passionate about performing improvised comedy
theater with Impro Melbourne (http://www.impromelbourne.com.au/) and flying light aircraft.
Kevin’s personal blog is Yes, I’m Canadian (http://yesimcanadian.com/).
About the Technical Editor
Andrew Tetlaw has been tinkering with web sites as a web developer since 1997. At SitePoint
he is dedicated to making the world a better place through the technical editing of SitePoint
books, kits, articles, and newsletters. He is also a busy father of five, enjoys coffee, and often
neglects his blog at http://tetlaw.id.au/.
About SitePoint
SitePoint specializes in publishing fun, practical, and easy-to-understand content for Web
professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles,
and community forums.
v
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
To my parents, Cheryl and
Richard, for making all this
possible.
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Where to Find Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
The Book’s Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi
Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Your Own Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Windows Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
All-in-one Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Installing Individual Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Mac OS X Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
All-in-one Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Installing Individual Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Linux Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Post-Installation Set-up Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
What to Ask Your Web Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Your First PHP Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Full Toolbox, Dirty Hands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Chapter 2 Introducing MySQL . . . . . . . . . . . . . . . . . . . . . 53
An Introduction to Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Logging On to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Structured Query Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Creating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Inserting Data into a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Viewing Stored Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Modifying Stored Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Deleting Stored Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Let PHP Do the Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Chapter 3 Introducing PHP . . . . . . . . . . . . . . . . . . . . . . . . . 73
Basic Syntax and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Variables, Operators, and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
User Interaction and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Hiding the Seams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Avoid Advertising Your Technology Choices . . . . . . . . . . . . . . . . . . 104
Use PHP Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Many Templates, One Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Bring On the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Chapter 4 Publishing MySQL Data on the
Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Connecting to MySQL with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Sending SQL Queries with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
x
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Handling SELECT Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Inserting Data into the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Mission Accomplished . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Chapter 5 Relational Database Design . . . . . . . . . 151
Giving Credit Where Credit is Due . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Rule of Thumb: Keep Entities Separate . . . . . . . . . . . . . . . . . . . . . . . . . . 153
SELECT with Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Simple Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Many-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
One for Many, and Many for One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Chapter 6 Structured PHP Programming . . . . . . 171
Include Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Including HTML Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Including PHP Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Types of Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Shared Include Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Custom Functions and Function Libraries . . . . . . . . . . . . . . . . . . . . . . . . 184
Variable Scope and Global Access . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Structure in Practice: Template Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . 191
The Best Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Chapter 7 A Content Management System . . . . 197
The Front Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Managing Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Deleting Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Adding and Editing Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
xi
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Managing Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Managing Jokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Searching for Jokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Adding and Editing Jokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Deleting Jokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Chapter 8 Content Formatting with Regular
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
String Replacement with Regular Expressions . . . . . . . . . . . . . . . . . . . . 247
Boldface and Italic Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Paragraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Matching Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Real World Content Submission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Chapter 9 Cookies, Sessions, and Access
Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
PHP Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
A Simple Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Database Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Controller Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Function Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Managing Passwords and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
A Challenge: Joke Moderation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
xii
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
The Sky’s the Limit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Chapter 10 MySQL Administration . . . . . . . . . . . . . . . 313
phpMyAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Backing Up MySQL Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Database Backups Using mysqldump . . . . . . . . . . . . . . . . . . . . . . . 319
Incremental Backups Using Binary Logs . . . . . . . . . . . . . . . . . . . . . 321
MySQL Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Granting Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Revoking Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Access Control Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Locked Out? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Checking and Repairing MySQL Data Files . . . . . . . . . . . . . . . . . . . . . . . 332
Better Safe than Sorry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Chapter 11 Advanced SQL Queries . . . . . . . . . . . . . . . 337
Sorting SELECT Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Setting LIMITs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
LOCKing TABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Column and Table Name Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
GROUPing SELECT Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
LEFT JOINs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Limiting Results with HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Chapter 12 Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Semi-dynamic Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Handling File Uploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Assigning Unique Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
xiii
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info
Recording Uploaded Files in the Database . . . . . . . . . . . . . . . . . . . . . . . . 369
Binary Column Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Storing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Viewing Stored Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Large File Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
MySQL Packet Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
PHP Script Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
The End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Appendix A MySQL Syntax Reference . . . . . . . . . . . . 389
SQL Statements Implemented in MySQL . . . . . . . . . . . . . . . . . . . . . . . . . 389
ALTER TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
ANALYZE TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
CREATE DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
CREATE INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
CREATE TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
DESCRIBE/DESC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
DROP DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
DROP INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
DROP TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
EXPLAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
GRANT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
LOAD DATA INFILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
LOCK/UNLOCK TABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
OPTIMIZE TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
RENAME TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
REPLACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
xiv
Licensed to botuongxulang@yahoo.com
www.it-ebooks.info

Không có nhận xét nào:

Đăng nhận xét