PHP and MySQL by Example [With CDROM] 0131875086, 9780131875081

776 107 10MB

English Pages 878 [910] Year 2006;2010

Report DMCA / Copyright

DOWNLOAD FILE

PHP and MySQL by Example [With CDROM]
 0131875086, 9780131875081

Table of contents :
Cover......Page 1
Contents......Page 6
Preface......Page 24
Acknowledgments......Page 25
1.1.1 Static Web Sites......Page 26
1.1.2 Dynamic Web Sites......Page 27
1.1.3 What Is Open Source?......Page 28
1.2 About PHP......Page 29
1.2.1 Where to Get PHP and Documentation......Page 30
1.3.2 Features of MySQL......Page 32
1.3.4 Advantages of MySQL and PHP......Page 34
1.4.2 What's Next?......Page 36
2.1 The Life Cycle of a Web Page......Page 38
2.1.1 Analysis of a Web Page......Page 39
2.2.1 The Steps of Writing a PHP Script......Page 40
2.3.1 PHP and HTML Are Different Languages......Page 46
2.3.2 Statements, Whitespace, and Line Breaks......Page 48
2.3.3 Comments......Page 50
2.3.4 Using PHP Functions......Page 52
2.4 Review......Page 56
2.4.3 Script Execution at the Command Line......Page 57
2.4.5 PHP Command-Line Options......Page 59
2.4.6 The php.ini File......Page 60
2.5.1 What You Should Know......Page 62
Chapter 2 Lab......Page 63
3.1.2 A Note to Nonprogrammers......Page 66
3.1.3 PHP Syntax and Constructs......Page 67
3.2.1 What's Next?......Page 82
4.1 Data Types......Page 84
4.1.1 Numeric Literals......Page 85
4.1.2 String Literals and Quoting......Page 87
4.1.4 Special Data Types......Page 93
4.2.1 Definition and Assignment......Page 95
4.2.3 Declaring and Initializing Variables......Page 96
4.2.4 Displaying Variables......Page 100
4.2.5 Variables and Mixed Data Types......Page 103
4.2.6 Concatenation and Variables......Page 104
4.2.7 References......Page 105
4.2.8 Variable Variables (Dynamic Variables)......Page 107
4.2.9 Scope of Variables......Page 109
4.2.10 Managing Variables......Page 110
4.2.11 Introduction to Form Variables......Page 115
4.3.1 What Is a Constant?......Page 124
4.3.2 Creating Constants with the define() Function......Page 125
4.3.4 Predefined and "Magic" Constants......Page 127
4.4.1 What You Should Know......Page 129
Chapter 4 Lab......Page 130
5.1 About PHP Operators and Expressions......Page 132
5.1.2 Precedence and Associativity......Page 133
5.1.3 Arithmetic Operators......Page 137
5.1.4 Short Circuit Assignment Operators......Page 138
5.1.5 Autoincrement and Autodecrement Operators......Page 140
5.1.6 Some Useful Math Functions......Page 142
5.1.7 Casting Operators......Page 143
5.1.8 Concatention Operator......Page 145
5.1.9 Comparison Operators......Page 147
5.1.10 Comparing Numbers......Page 149
5.1.11 Comparing Strings......Page 150
5.1.12 Logical Operators......Page 151
5.1.13 The Conditional Operator......Page 163
5.1.14 Bitwise Operators......Page 165
5.1.16 Error Control Operator......Page 168
5.2.1 What You Should Know......Page 170
Chapter 5 Lab......Page 171
6.1 What Is a String?......Page 174
6.1.1 Quotes......Page 175
6.1.2 String Operators......Page 177
6.2.1 Formatting and Printing Strings......Page 180
6.2.2 Formatting Numbers and Money......Page 185
6.2.3 Finding the Length of a String......Page 188
6.2.5 Changing the Case of Strings......Page 189
6.2.6 Comparing Strings......Page 192
6.2.7 Finding Similarities in Strings......Page 199
6.2.8 Splitting a String......Page 204
6.2.10 Trimming and Padding Strings......Page 206
6.2.11 Search and Replace......Page 210
6.2.12 Finding a Position in a String......Page 214
6.2.13 Extracting Pieces of a String—Substrings......Page 216
6.2.14 Special Characters and Strings......Page 225
6.2.15 Working with HTML Special Characters......Page 235
6.3 Other String Functions......Page 239
6.4.1 What You Should Know......Page 243
Chapter 6 Lab......Page 244
7.1 Control Structures, Blocks, and Compound Statements......Page 246
7.1.1 Conditionals......Page 247
7.2.1 The while Loop......Page 257
7.2.2 The do/while Loop......Page 259
7.2.3 The for Loop......Page 260
7.2.5 Loop Control with break and continue......Page 264
7.3.1 What You Should Know......Page 268
Chapter 7 Lab......Page 269
8.1 What Is an Array?......Page 272
8.1.1 Creating and Naming an Array......Page 276
8.1.2 Accessing an Array's Elements (Values)......Page 282
8.1.3 Printing an Array......Page 287
8.1.4 Using Loops to Access Array Elements......Page 291
8.1.5 Checking If an Array Exists......Page 299
8.1.6 Creating Strings from Arrays and Arrays from Strings......Page 300
8.1.7 Finding the Size of an Array......Page 303
8.1.8 Extracting Keys and Values from Arrays......Page 306
8.1.9 Creating Variables from Array Elements......Page 311
8.1.10 Multidimensional Arrays......Page 318
8.1.11 Sorting Arrays......Page 323
8.1.12 Randomizing an Array......Page 331
8.2.1 Removing an Array and Its Elements......Page 335
8.2.2 Adding Elements to an Array......Page 340
8.2.3 Copying Elements of an Array......Page 345
8.2.4 Combining and Merging Arrays......Page 347
8.2.5 Array Operators......Page 352
8.2.6 More Array Functions......Page 355
8.3.1 What You Should Know......Page 360
Chapter 8 Lab......Page 361
9.1 What Is a Function?......Page 362
9.1.1 Function Declaration, Definition, and Invocation......Page 363
9.1.2 Passing Arguments......Page 366
9.1.3 Return Values......Page 376
9.1.4 Using Callback Functions......Page 383
9.1.5 Scope......Page 387
9.1.6 Nesting Functions......Page 393
9.1.7 Recursive Functions......Page 395
9.1.8 Function Libraries—Requiring and Including......Page 398
9.2.1 What You Should Know......Page 401
Chapter 9 Lab......Page 402
10.2 Review of HTML Forms......Page 404
10.2.1 The Browser's Role......Page 405
10.2.2 The Server's Role......Page 408
10.2.3 Creating HTML Forms......Page 409
10.3 PHP and Forms......Page 415
10.3.1 The register_globals Directive......Page 416
10.3.2 PHP Superglobals for Retrieving Form Data......Page 417
10.3.4 Strange Characters in Form Field Names and User Input......Page 418
10.3.5 Form Parameters the $_REQUEST Array......Page 421
10.3.6 Form Parameters and the Medium Style......Page 425
10.3.8 Processing Forms with Multiple Selections......Page 431
10.3.9 Forms Using an Image Button......Page 435
10.3.10 Self-Processing HTML Forms......Page 438
10.3.11 Using Hidden Fields......Page 440
10.3.12 Redirecting the User......Page 442
10.3.13 Uploading Files......Page 446
10.3.14 Sticky Forms......Page 452
10.3.15 Where to Get Information About Superglobal Arrays......Page 456
10.3.16 How to Get Server Information......Page 457
10.3.17 How to Get Information About the Environment......Page 462
10.4.1 What You Should Know......Page 463
Chapter 10 Lab......Page 464
11.1.2 UNIX/Linux Permissions......Page 466
11.1.3 Windows Permissions......Page 469
11.2 The Web Server, PHP, and Permissions......Page 471
11.2.1 PHP Built-In Functions......Page 472
11.2.3 Opening a File......Page 473
11.2.4 Opening a File for Reading......Page 476
11.2.5 Positioning the File Pointer......Page 483
11.2.6 Opening a URL for Reading......Page 488
11.2.7 Reading from Files Without a Filehandle......Page 489
11.2.8 Opening a File for Writing and Appending......Page 496
11.2.9 File Checks......Page 500
11.2.10 Creating, Copying, Renaming, and Deleting Files......Page 506
11.3.1 Opening and Reading from a Directory......Page 508
11.3.2 Getting Path Information......Page 510
11.3.3 Changing and Getting the Current Directory......Page 511
11.4 Managing Content with Include Files......Page 512
11.4.1 A Real-World Example......Page 513
11.5.1 What You Should Know......Page 519
11.5.2 What's Next?......Page 520
12.1 What Is a Regular Expression?......Page 522
12.2 Pattern-Matching Functions......Page 524
12.2.1 Finding a Pattern......Page 525
12.2.2 Searching and Replacing......Page 531
12.2.3 Getting Control—The RegEx Metacharacters......Page 542
12.2.4 Searching for Patterns in Text Files......Page 579
12.2.5 Form Validation with PHP......Page 581
12.2.6 Help on the Web......Page 587
12.3.2 What's Next?......Page 590
Chapter 12 Lab......Page 591
13.1 About Databases......Page 592
13.1.2 Talking to the Database......Page 593
13.1.3 MySQL Strengths and Weaknesses......Page 594
13.2 The Anatomy of a Relational Database......Page 595
13.2.3 Tables......Page 596
13.2.4 Records and Fields......Page 597
13.2.5 Primary Key and Indexes......Page 599
13.3 Connecting to the Database......Page 600
13.3.1 MySQL Command-Line Options......Page 601
13.3.2 Graphical User Tools......Page 604
13.4.1 Logging into the Database Server......Page 607
13.4.2 Finding the Databases......Page 608
13.4.3 The "user" Table......Page 610
13.4.4 The "db" Table......Page 611
13.4.5 The "host" Table......Page 612
13.4.6 A Real-World Example......Page 613
13.4.7 The Grant and Revoke Commands......Page 614
13.4.8 Creating and Dropping a Database......Page 615
13.4.9 Some Useful MySQL Functions......Page 617
13.5 Chapter Summary......Page 618
13.5.2 What's Next?......Page 619
14.1 What Is SQL?......Page 620
14.1.2 Executing SQL Statements......Page 621
14.1.3 About SQL Commands/Queries......Page 622
14.1.4 SQL and the Database......Page 625
14.1.5 SQL Database Tables......Page 626
14.2 SQL Data Manipulation Language (DML)......Page 628
14.2.1 The SELECT Command......Page 629
14.2.2 The INSERT Command......Page 642
14.2.3 The UPDATE Command......Page 643
14.2.4 The DELETE Statement......Page 644
14.3.1 Creating the Database......Page 645
14.3.2 SQL Data Types......Page 646
14.3.3 Creating a Table......Page 648
14.3.4 Creating a Key......Page 650
14.3.5 Relations......Page 653
14.3.6 Altering a Table......Page 656
14.4 SQL Functions......Page 658
14.4.1 Numeric Functions......Page 659
14.4.2 String Functions......Page 662
14.4.3 Date and Time Functions......Page 663
14.5.1 What You Should Know......Page 667
Chapter 14 Lab......Page 668
15.1.1 Connecting to the Database Server......Page 672
15.1.2 Choosing the Database......Page 674
15.1.3 Executing SQL Statements (INSERT, UPDATE, DELETE)......Page 675
15.1.4 Retrieving the Query Results (SELECT)......Page 678
15.1.5 Other Useful MySQL Functions......Page 682
15.2.1 Step 1: Designing the Database......Page 688
15.2.2 Step 2: Posting a Message Page......Page 690
15.2.3 Step 3: Listing All Messages......Page 694
15.2.4 Example Summary......Page 695
Chapter 15 Lab......Page 696
16.2 What Are Cookies?......Page 698
16.2.1 Cookie Ingredients......Page 699
16.2.2 The Attributes of a Cookie......Page 700
16.3.1 Creating Cookies with the setcookie() Function......Page 702
16.3.2 Tracking Visitors with Cookies......Page 708
16.3.3 Extending the Life of a Cookie......Page 713
16.3.4 Buffering and HTTP Headers......Page 714
16.3.5 Deleting a Cookie......Page 718
16.4 What Is a Session?......Page 719
16.4.1 Where to Store Sessions......Page 721
16.4.2 Starting a Cookie-Based Session......Page 723
16.4.3 Registering a Session......Page 724
16.4.4 Saving Arrays in a Session......Page 727
16.4.5 Session Cookie Functions and Configuration Options......Page 731
16.4.6 Setting Preferences with Sessions......Page 733
16.4.7 Naming Sessions......Page 740
16.4.8 Sessions Without Cookies......Page 742
16.4.9 Passing Session IDs with a Link......Page 746
16.4.10 Changing the Session ID......Page 751
16.4.11 Ending a Session......Page 752
16.4.12 Session Runtime Configuration......Page 755
16.4.13 Implementing a Login System with Sessions......Page 756
16.5 Chapter Summary......Page 761
16.5.1 What You Should Know......Page 762
Chapter 16 Lab......Page 763
17.1 What Are Objects?......Page 764
17.1.1 Objects and Classes......Page 765
17.2.2 Instantiating the Class......Page 766
17.2.3 Creating a Complete Class......Page 771
17.2.4 Displaying an Object......Page 772
17.2.5 Functions for Getting Class Information......Page 773
17.2.6 Encapsulation and Information Hiding......Page 774
17.2.7 Class Members and Scope......Page 775
17.2.8 Magic Methods......Page 778
17.2.9 Inheritance......Page 788
17.2.10 Overriding Methods......Page 791
17.2.11 Protected Access......Page 793
17.3.1 final Classes and Methods......Page 797
17.3.2 Static Members......Page 798
17.3.3 Class Constants......Page 799
17.3.4 Reusing a Class......Page 801
17.4.1 What You Should Know......Page 805
Chapter 17 Lab......Page 806
A.2 The Public and the Private......Page 808
A.3.1 Creating the Database......Page 810
A.3.2 Administration Pages......Page 811
A.3.3 Public Pages......Page 822
A.3.4 Securing Pages with Login......Page 828
A.4.1 Where to Find the Files for the Canvas Gallery Site......Page 830
A.4.2 Installing the MySQL Database......Page 831
A.5 Conclusion......Page 833
B.1 The Mail Server......Page 834
B.3 Runtime Configuration Options......Page 835
B.5 Sending a Simple E-Mail Message......Page 836
B.6 Example: Sending an HTML Message......Page 838
B.6.1 Sending a Message with an Attachment......Page 839
C.1.1 The date() Function......Page 844
C.1.2 The strftime() Function......Page 847
C.2.1 The time() Function......Page 849
C.2.2 The mktime() Function......Page 850
C.2.3 Creating a UNIX Timestamp from a String......Page 853
C.3 Getting the Date and Time......Page 854
C.3.1 Validating Dates......Page 856
D.1.1 Network Security and SSL......Page 858
D.2 Securing PHP and MySQL......Page 859
D.2.1 Basic PHP Security Principles......Page 860
D.3.2 Fix the Parse Errors First......Page 866
D.3.3 Diagnostic Print Statements......Page 868
D.3.4 Fixing SQL Errors......Page 870
D.3.5 More on Debugging......Page 872
E.2 Installing Apache on Windows......Page 874
E.3 Installing PHP on Windows......Page 875
E.6 Configuring Apache to use PHP Module (All Platforms)......Page 876
E.6.1 Testing PHP and Apache Installation......Page 877
E.7 Configuring php.ini (All Platforms)......Page 878
E.11 Read the Manual......Page 879
A......Page 880
B......Page 882
C......Page 883
D......Page 885
E......Page 886
F......Page 887
H......Page 888
I......Page 889
L......Page 890
M......Page 891
N......Page 892
O......Page 893
P......Page 894
R......Page 896
S......Page 897
T......Page 901
V......Page 902
X–Z......Page 903

Polecaj historie