Tải bản đầy đủ (.pdf) (10 trang)

Học php, mysql và javascript - p 1 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.14 MB, 10 trang )

Learning PHP, MySQL, and
JavaScript
Robin Nixon
Beijing

Cambridge

Farnham

Köln

Sebastopol

Taipei

Tokyo
Learning PHP, MySQL, and JavaScript
by Robin Nixon
Copyright © 2009 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 (). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or
Editor: Andy Oram
Production Editor: Sumita Mukherji


Copyeditor: Nancy Kotary
Proofreader: Kiel Van Horn
Indexer: Ellen Troutman Zaig
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
July 2009:
First Edition.
Nutshell Handbook,
the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Learning
PHP, MySQL, and JavaScript, the image of sugar gliders, and related trade
dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN: 978-0-596-15713-5
[M]
1246467361
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
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, and JavaScript 5
Using PHP 5
Using MySQL 6
Using JavaScript 7
The Apache Web Server 8
About Open Source 9
Bringing It All Together 9
Test Your Knowledge: Questions 11
2. Setting Up a Development Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
What Is a WAMP, MAMP, or LAMP? 13
Installing a WAMP on Windows 14
Overcoming Installation Problems 14
Testing the Installation 16
Alternative WAMPs 18
Installing a MAMP on Mac OS X 19
Some Final Tweaking 24
Other Alternatives 25
Installing a LAMP on Linux 25
Working Remotely 26
Logging In 27
Using FTP 27
Using a Program Editor 28
Using an IDE 30
Test Your Knowledge: Questions 32
iii
3. Introduction to PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Incorporating PHP Within HTML 33
Calling the PHP Parser 34
This Book’s Examples 35

The Structure of PHP 36
Using Comments 36
Basic Syntax 37
Understanding Variables 38
Operators 42
Variable Assignment 45
Multiple-Line Commands 47
Variable Typing 49
Constants 50
The Difference Between the echo and print Commands 51
Functions 52
Variable Scope 53
Test Your Knowledge: Questions 58
4. Expressions and Control Flow in PHP . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Expressions 61
Literals and Variables 62
Operators 63
Operator Precedence 64
Associativity 66
Relational Operators 67
Conditionals 70
The if Statement 71
The else Statement 72
The elseif Statement 73
The switch Statement 74
The ? Operator 77
Looping 78
while Loops 78
do while Loops 80

for Loops 81
Breaking Out of a Loop 83
The continue Statement 84
Implicit and Explicit Casting 84
PHP Dynamic Linking 85
Dynamic Linking in Action 86
Test Your Knowledge: Questions 87
iv | Table of Contents
5. PHP Functions and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
PHP Functions 90
Defining a Function 91
Returning a Value 92
Returning an Array 93
Passing by Reference 94
Returning Global Variables 95
Recap of Variable Scope 96
Including and Requiring Files 96
The include Statement 96
Using include_once 97
Using require and require_once 97
PHP Version Compatibility 98
PHP Objects 98
Terminology 99
Declaring a Class 100
Creating an Object 101
Accessing Objects 101
Constructors 104
Writing Methods 105
Declaring Properties 106
Declaring Constants 107

Property and Method Scope in PHP 5 107
Inheritance 109
Test Your Knowledge: Questions 113
6. PHP Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Basic Access 115
Numerically Indexed Arrays 115
Associative Arrays 117
Assignment Using the array Keyword 118
The foreach as Loop 119
Multidimensional Arrays 121
Using Array Functions 123
is_array() 123
count() 124
sort() 124
shuffle() 124
explode() 125
extract() 125
compact() 126
reset() 127
end() 128
Table of Contents | v
Test Your Knowledge: Questions 128
7. Practical PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Using printf 129
Precision Setting 131
String Padding 132
Using sprintf 133
Date and Time Functions 133
Date Constants 136
Using checkdate 136

File Handling 137
Checking Whether a File Exists 137
Creating a File 137
Reading from Files 139
Copying Files 139
Moving a File 140
Deleting a File 140
Updating Files 141
Locking Files for Multiple Accesses 142
Reading an Entire File 143
Uploading Files 144
System Calls 149
XHTML 151
The Benefits of XHTML 151
XHTML Versions 151
What’s Different? 152
HTML 4.01 Document Types 153
XHTML 1.0 Document Types 153
XHTML Validation 154
Test Your Knowledge: Questions 155
8. Introduction to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
MySQL Basics 157
Summary of Database Terms 158
Accessing MySQL via the Command Line 158
Starting the Command-Line Interface 159
Using the Command-Line Interface 163
MySQL Commands 164
Data Types 168
Indexes 177
Creating an Index 178

Querying a MySQL Database 183
Joining Tables Together 192
vi | Table of Contents
Using Logical Operators 194
MySQL Functions 194
Accessing MySQL via phpMyAdmin 195
Windows Users 195
Mac OS X Users 195
Linux Users 195
Using phpMyAdmin 197
Test Your Knowledge: Questions 198
9. Mastering MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Database Design 201
Primary Keys: The Keys to Relational Databases 202
Normalization 203
First Normal Form 204
Second Normal Form 206
Third Normal Form 208
When Not to Use Normalization 210
Relationships 211
One-to-One 211
One-to-Many 212
Many-to-Many 212
Databases and Anonymity 214
Transactions 214
Transaction Storage Engines 215
Using BEGIN 216
Using COMMIT 216
Using ROLLBACK 216
Using EXPLAIN 217

Backing Up and Restoring 218
Using mysqldump 219
Creating a Backup File 220
Restoring from a Backup File 222
Dumping Data in CSV Format 222
Planning Your Backups 223
Test Your Knowledge: Questions 223
10. Accessing MySQL Using PHP . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Querying a MySQL Database with PHP 225
The Process 225
Creating a Login File 226
Connecting to MySQL 227
A Practical Example 232
The $_POST Array 234
Table of Contents | vii
Deleting a Record 235
Displaying the Form 236
Querying the Database 236
Running the Program 237
Practical MySQL 238
Creating a Table 238
Describing a Table 239
Dropping a Table 240
Adding Data 240
Retrieving Data 241
Updating Data 242
Deleting Data 242
Using AUTO_INCREMENT 243
Performing Additional Queries 244

Preventing SQL Injection 245
Preventing HTML Injection 248
Test Your Knowledge: Questions 250
11. Form Handling . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Building Forms 251
Retrieving Submitted Data 253
register_globals: An Old Solution Hangs On 254
Default Values 254
Input Types 256
Text Boxes 256
Text Areas 256
Checkboxes 257
Radio Buttons 259
Hidden Fields 260
Select 260
Labels 262
Sanitizing Input 262
An Example Program 264
Test Your Knowledge: Questions 266
12. Templating with Smarty . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Why Smarty? 270
Installation 270
Creating Scripts 271
Creating Templates 272
A Practical Example 272
Test Your Knowledge: Questions 277
viii | Table of Contents

×