Đăng ký Đăng nhập
Trang chủ Ngoại ngữ Kiến thức tổng hợp Php 6 fast & easy web development...

Tài liệu Php 6 fast & easy web development

.PDF
593
116
116

Mô tả:

PHP 6 FAST & EASY WEB DEVELOPMENT Julie Meloni | Matt Telles Course Technology PTR A part of Cengage Learning Australia, Brazil, Japan, Korea, Mexico, Singapore, Spain, United Kingdom, United States PHP 6 Fast & Easy Web Development Julie Meloni, Matt Telles Publisher and General Manager, Course Technology PTR: Stacy L. Hiquet Associate Director of Marketing: Sarah Panella Manager of Editorial Services: Heather Talbot © 2008 Course Technology, a part of Cengage Learning. ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored, or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher. Marketing Manager: Mark Hughes Acquisitions Editor: Mitzi Koontz Project and Copy Editor: Marta Justak Technical Reviewer: Jaelle Scheuerman PTR Editorial Services Coordinator: Erin Johnson Interior Layout Tech: Bill Hartman Cover Designer: Mike Tanamachi Indexer: Larry Sweazy Proofreader: Gene Redding For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support Center, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at cengage.com/permissions Further permissions questions can be emailed to [email protected] Microsoft, Windows, and Internet Explorer are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Netscape is a registered trademark of Netscape Communications Corporation in the U.S. and other countries. PHP is copyrighted by The PHP Group, and is released under the PHP License. MySQL is copyrighted by MySQL AB and is released under the GNU General Public License. All other trademarks are the property of their respective owners. Library of Congress Control Number: 2007938248 ISBN-13: 978-1-59863-471-6 ISBN-10: 1-59863-471-2 eISBN-10: 1-59863-669-3 Course Technology 25 Thomson Place Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in the United States of America 1 2 3 4 5 6 7 11 10 09 08 Acknowledgments Thanks as always to the PHP Group, Zend Technologies, the Apache Software Foundation, and MySQL AB for creating and maintaining such wonderful and accessible products for all users. Thanks to every single PHP user and developer, because without you I wouldn’t have anything to write about. Great thanks to the all the editors who worked with me on all the editions of this book! Enormous thanks to everyone at i2i Interactive, for their never-ending support and encouragement. Matt would like to thank his wonderful editor, Marta, and phenomenal other half, Teresa, for getting him through this book. This page intentionally left blank About the Authors Julie Meloni is the technical director for i2i Interactive, a multimedia company located in Los Altos, CA. She’s been developing Web-based applications since the Web first saw the light of day and remembers the excitement surrounding the first GUI Web browser. She is the author of several books and articles on Web-based programming languages and database topics, and you can find translations of her work in several languages, including Chinese, Italian, Portuguese, Polish, and even Serbian. Matt Telles is a senior consultant working in the software development world. He lives, breathes, and works with anything anyone pays him to while pining away for his beloved DEC-1091. This page intentionally left blank Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix PART I GETTING STARTED . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Installing and Configuring MySQL . . . . . . . . . . . . . . . . 3 Various MySQL Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Installing MySQL on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Testing Your MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . . . 12 Installing MySQL for Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Testing Your MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter 2 Installing Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Installing Apache for Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Configuring Apache on Windows . . . . . . . . . . . . . . . . . . . . . . . . 29 Starting and Connecting to Apache . . . . . . . . . . . . . . . . . . . . . . 31 Installing Apache for Linux/UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Configuring Apache on Linux/UNIX . . . . . . . . . . . . . . . . . . . . . . 34 Starting and Connecting to Apache . . . . . . . . . . . . . . . . . . . . . . 36 viii CONTENTS Chapter 3 Installing PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Installing PHP for Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Configuring Apache to Use PHP. . . . . . . . . . . . . . . . . . . . . . . . . 41 Testing the PHP Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Installing PHP for Linux/UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Configuring Apache to Use PHP. . . . . . . . . . . . . . . . . . . . . . . . . 46 Testing the PHP Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 PART II THE ABSOLUTE BASICS OF CODING IN PHP . . . . 49 Chapter 4 Mixing PHP and HTML. . . . . . . . . . . . . . . . . . . . . . . . 51 How PHP Is Parsed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 PHP Start and End Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Code Cohabitation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 The Importance of the Instruction Terminator . . . . . . . . . . . . . . 57 Escaping Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Commenting Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Chapter 5 Introducing Variables and Operators . . . . . . . . . . . . 65 What’s a Variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Naming Your Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 PHP Variable and Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 What’s an Operator? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 CONTENTS Chapter 6 Using PHP Variables . . . . . . . . . . . . . . . . . . . . . . . . . 85 Getting Variables from Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Creating a Calculation Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Creating the Calculation Script. . . . . . . . . . . . . . . . . . . . . . . . . . 89 Submitting Your Form and Getting Results . . . . . . . . . . . . . . . . 91 HTTP Environment Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Retrieving and Using REMOTE_ADDR . . . . . . . . . . . . . . . . . . . . 93 Retrieving and Using HTTP_USER_AGENT . . . . . . . . . . . . . . . . 95 PART III START WITH THE SIMPLE STUFF . . . . . . . . . . . . . . 97 Chapter 7 Displaying Dynamic Content . . . . . . . . . . . . . . . . . . . 99 Displaying Browser-Specific HTML . . . . . . . . . . . . . . . . . . . . . . . . 100 Displaying Platform-Specific HTML. . . . . . . . . . . . . . . . . . . . . . . . 103 Working with String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Creating an Input Form. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Creating a Script to Display Form Values . . . . . . . . . . . . . . . . . 109 Submitting Your Form and Getting Results . . . . . . . . . . . . . . . 111 Redirecting to a New Location . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Creating a Redirection Form. . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Creating the Redirection Script and Testing It . . . . . . . . . . . . . 115 Chapter 8 Sending E-Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Using an SMTP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 SMTP-Related Changes in php.ini . . . . . . . . . . . . . . . . . . . . . . 119 A Simple Feedback Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Creating the Feedback Form . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Creating a Script to Mail Your Form . . . . . . . . . . . . . . . . . . . . . 122 Submitting Your Form and Getting Results . . . . . . . . . . . . . . . 125 ix x CONTENTS A Feedback Form with Custom Error Messages . . . . . . . . . . . . . 127 Creating the Initial Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Adding Error Checking to the Script . . . . . . . . . . . . . . . . . . . . 129 Submitting Your Form and Getting Results . . . . . . . . . . . . . . . 134 Saving the Values if You Make an Error . . . . . . . . . . . . . . . . . . 136 Chapter 9 Using Your File System . . . . . . . . . . . . . . . . . . . . . . 139 File Paths and Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Displaying Directory Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Working with fopen() and fclose() . . . . . . . . . . . . . . . . . . . . . . . . . 143 Creating a New File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Appending Data to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Reading Data from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Sending File Contents via E-Mail . . . . . . . . . . . . . . . . . . . . . . . 155 File System Housekeeping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Copying Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Chapter 10 Uploading Files to Your Web Site . . . . . . . . . . . . . . 165 Checking Your php.ini File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Understanding the Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Creating the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Creating the Upload Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Uploading a File Using Your Form and Script . . . . . . . . . . . . . . . . 172 CONTENTS PART IV GETTING TO KNOW YOUR MYSQL DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Chapter 11 Establishing a Connection and Poking Around . . . . 177 Working with User Privileges in MySQL . . . . . . . . . . . . . . . . . . . . 178 Creating a New User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Connecting to MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Breaking Your Connection Script . . . . . . . . . . . . . . . . . . . . . . . 182 Listing Databases on a Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Listing Tables in a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Creating a New Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Deleting a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Chapter 12 Creating a Database Table. . . . . . . . . . . . . . . . . . . . 197 Planning for Your Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Basic MySQL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Defining Your Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 The Importance of Unique Fields . . . . . . . . . . . . . . . . . . . . . . . 201 A Two-Step Form Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Step 1: Number of Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Step 2: Defining Your Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Starting the Table Creation Process . . . . . . . . . . . . . . . . . . . . . 208 Creating the Table-Creation Script . . . . . . . . . . . . . . . . . . . . . . . . 210 Create That Table! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Chapter 13 Inserting Data into the Table . . . . . . . . . . . . . . . . . . 217 Creating the Record Addition Form . . . . . . . . . . . . . . . . . . . . . . . 218 Creating the Record Addition Script. . . . . . . . . . . . . . . . . . . . . . . 222 Populating Your Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 xi xii CONTENTS Chapter 14 Selecting and Displaying Data . . . . . . . . . . . . . . . . . 231 Planning and Creating Your Administrative Menu . . . . . . . . . . . . 232 Selecting Data from the my_music Table . . . . . . . . . . . . . . . . . . . 233 Displaying Records Ordered by ID . . . . . . . . . . . . . . . . . . . . . . 234 Displaying Records Ordered by Date Acquired . . . . . . . . . . . . 237 Displaying Records Ordered by Title . . . . . . . . . . . . . . . . . . . . 238 Displaying Records Ordered by Artist . . . . . . . . . . . . . . . . . . . 240 Displaying Records Ordered by Multiple Criteria . . . . . . . . . . 243 PART V USER AUTHENTICATION AND TRACKING . . . . . 245 Chapter 15 Database-Driven User Authentication . . . . . . . . . . . 247 Why Authenticate Anyone?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Creating the User Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Adding Users to Your Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Creating the User Addition Form and Script . . . . . . . . . . . . . . 250 Adding Some Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Creating the Login Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Creating the Authentication Script . . . . . . . . . . . . . . . . . . . . . . . . 258 Trying to Authenticate Yourself. . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Chapter 16 Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 What Are Cookies? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Setting Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Counting Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Setting a Test Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Using Cookie Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Using Cookies with Authentication . . . . . . . . . . . . . . . . . . . . . 269 CONTENTS Chapter 17 Session Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Before You Begin…Checking php.ini . . . . . . . . . . . . . . . . . . . . . . 278 What’s a Session? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Understanding Session Variables . . . . . . . . . . . . . . . . . . . . . . . . . 279 Starting a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Registering and Modifying Session Variables. . . . . . . . . . . . . . 282 Managing User Preferences with Sessions . . . . . . . . . . . . . . . . . . 284 Starting a Session and Registering Defaults. . . . . . . . . . . . . . . 284 Making Preference Changes. . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Displaying Changes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 PART VI CREATING YOUR OWN CONTACT MANAGEMENT SYSTEM . . . . . . . . . . . . . . . . . . . 293 Chapter 18 Planning Your System . . . . . . . . . . . . . . . . . . . . . . . 295 Planning and Creating the Administration Menu . . . . . . . . . . . . . 296 Logging In to the Administration Menu . . . . . . . . . . . . . . . . . . 301 Defining the my_contacts Table . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Modifying the Table-Creation Scripts . . . . . . . . . . . . . . . . . . . . 304 Creating the my_contacts Table . . . . . . . . . . . . . . . . . . . . . . . . 309 Chapter 19 Adding Contacts . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Creating the Record-Addition Form . . . . . . . . . . . . . . . . . . . . . . . 314 Creating the Record-Addition Script . . . . . . . . . . . . . . . . . . . . . . 319 Populating Your Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Chapter 20 Modifying Contacts . . . . . . . . . . . . . . . . . . . . . . . . . 327 Creating the Record-Selection Form . . . . . . . . . . . . . . . . . . . . . . 328 Creating the Record-Modification Form . . . . . . . . . . . . . . . . . . . . 333 Creating the Record-Modification Script . . . . . . . . . . . . . . . . . . . 338 Modifying Contacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 xiii xiv CONTENTS Chapter 21 Deleting Contacts . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Using the Record-Selection Form . . . . . . . . . . . . . . . . . . . . . . . . . 346 Creating the Record-Deletion Form . . . . . . . . . . . . . . . . . . . . . . . 351 Creating the Record-Deletion Script. . . . . . . . . . . . . . . . . . . . . . . 355 Deleting Contacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Chapter 22 Working with Contacts . . . . . . . . . . . . . . . . . . . . . . 361 Modifying Your Administrative Menu . . . . . . . . . . . . . . . . . . . . . . 362 Showing the Number of Contacts . . . . . . . . . . . . . . . . . . . . . . 362 Displaying Today’s Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 Showing the Birthdays in the Current Month. . . . . . . . . . . . . . 372 Selecting Data from the my_contacts Table . . . . . . . . . . . . . . . . . 379 Displaying the Record List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Displaying Read-Only Records . . . . . . . . . . . . . . . . . . . . . . . . . 383 PART VII ADDITIONAL PROJECT EXAMPLES . . . . . . . . . . . 391 Chapter 23 Managing a Simple Mailing List . . . . . . . . . . . . . . . . 393 A Brief Word About Mailing List Software . . . . . . . . . . . . . . . . . . 394 Developing a Subscription Mechanism. . . . . . . . . . . . . . . . . . . . . 394 Creating the subscribers Table . . . . . . . . . . . . . . . . . . . . . . . . . 394 Creating the Subscription Form . . . . . . . . . . . . . . . . . . . . . . . . 396 Testing the Subscription Form . . . . . . . . . . . . . . . . . . . . . . . . . 403 Developing the Mailing Mechanism . . . . . . . . . . . . . . . . . . . . . . . 406 Creating the Newsletter Form . . . . . . . . . . . . . . . . . . . . . . . . . 406 Creating the Script to Mail Your Newsletter . . . . . . . . . . . . . . 407 Testing Your Mailing List Mechanism . . . . . . . . . . . . . . . . . . . . 410 Troubleshooting Your Mailing List Mechanism . . . . . . . . . . . . . 411 CONTENTS Chapter 24 Creating Custom Logs and Reports. . . . . . . . . . . . . 413 A Note About Apache Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . 414 Simple Access Counting with MySQL . . . . . . . . . . . . . . . . . . . . . . 415 Creating the Database Table . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Creating the Code Snippet. . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 Displaying the Count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 Creating Your Personal Access Report . . . . . . . . . . . . . . . . . . . 422 Chapter 25 Working with XML. . . . . . . . . . . . . . . . . . . . . . . . . . 433 What Is XML?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434 Basic XML Document Structure . . . . . . . . . . . . . . . . . . . . . . . . 434 Preparing to Use XML with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Parsing XML with PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 Parse and Display Content from XML Files . . . . . . . . . . . . . . . 439 PART VIII APPENDIXES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Appendix A Additional Configuration Options . . . . . . . . . . . . . . 445 Windows Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Linux Configuration Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 Appendix B Basic PHP Language Reference . . . . . . . . . . . . . . . . 451 PHP Start and End Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 Floats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Variables from HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Variables from Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 xv xvi CONTENTS Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Increment/Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . 457 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 if...else if...else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 foreach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Built-In Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 Database Connectivity Functions for MySQL. . . . . . . . . . . . . . 465 Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 File System Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 HTTP Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 mail() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Program Execution Functions. . . . . . . . . . . . . . . . . . . . . . . . . . 478 Regular Expression Functions. . . . . . . . . . . . . . . . . . . . . . . . . . 479 Session-Handling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Variable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Other Changes for PHP 6.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 CONTENTS Appendix C Writing Your Own Functions . . . . . . . . . . . . . . . . . . 487 The Structure of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 Returning Values from Functions . . . . . . . . . . . . . . . . . . . . . . . 488 Using Functions in Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 Using include() and require() . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 Appendix D Writing Your Own Classes and Objects . . . . . . . . . . 495 Working with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 Creating an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Object Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 Appendix E Database Normalization and SQL Reference. . . . . . 505 Understanding Database Normalization. . . . . . . . . . . . . . . . . . . . 506 Applying the Normal Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 Normalizing the my_contacts Table . . . . . . . . . . . . . . . . . . . . . 510 Other Normal Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Basic MySQL Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Creating or Dropping a Database . . . . . . . . . . . . . . . . . . . . . . 514 Creating or Dropping a Table . . . . . . . . . . . . . . . . . . . . . . . . . . 514 Altering a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 Inserting, Updating, or Replacing Within a Table. . . . . . . . . . . 515 Deleting from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Selecting from a Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Grouping, Ordering, and Selecting Unique Values . . . . . . . . . 520 Using the SHOW Command . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Appendix F Using SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 Examples of SQLite in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 Creating a Table and Storing Data with SQLite . . . . . . . . . . . . 525 Retrieving Items with SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . 526 Performing Other Tasks with SQLite . . . . . . . . . . . . . . . . . . . . 528 xvii xviii CONTENTS Appendix G Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 PHP Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 Mailing Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 User Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 MySQL Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 Apache Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 Introduction If you would have told me four years ago that this little book would be so popular as to warrant another edition, I would have laughed at you. But the style of this book, and its contents, has proven to be quite suitable for the beginning PHP programmer. The Fast & Easy Web Development style is a step-by-step, learn-byexample path to learning a new programming language—with pictures included! Unlike the verbose text-only chapters found in most programming books, the Fast & Easy Web Development style appeals to users who are new to PHP, and especially to programming in general. This edition takes into account feedback received from the other editions, but holds true to the original content structure and path to learning. In addition, all of the changes encompassed in the fifth and sixth releases of PHP are included. For example, the first three chapters are dedicated to getting Apache, MySQL, and PHP up and running on your Windows or Linux machine. You might be surprised at how simple it is, and how quickly you’ll be up and running—which is good because you need all three technologies to be working in order to continue with the lessons. In this edition, some chapters have been added to provide additional projects for practicing your new skills, and also to account for new elements present in version 6 of PHP. After completing this book, you will have a strong foundation in the basics of Web-based technologies and application design, and will be prepared to learn more advanced topics and programming methodologies. However, before jumping into all that, take a moment to familiarize yourself with PHP and why it is such a wonderful language to learn and use.
- Xem thêm -

Tài liệu liên quan