This page intentionally left blank PHP 5/MySQL Programming ANDY HARRIS © 2004 by Thomson Course Technology PTR. All rights reserved. No SVP, Thomson Course part of this book may be reproduced or transmitted in any form or by Technology PTR: any means, electronic or mechanical, including photocopying, record- Andy Shafran ing, or by any information storage or retrieval system without written permission from Thomson Course Technology PTR, except for the Publisher: inclusion of brief quotations in a review. Stacy L. Hiquet The Thomson Course Technology PTR logo and related trade dress are Senior Marketing Manager: trademarks of Thomson Course Technology PTR and may not be used without written permission. Sarah O’Donnell Microsoft, Windows, Internet Explorer, Notepad, VBScript, ActiveX, Marketiing Manager: and FrontPage are either registered trademarks or trademarks of Heather Hurley Microsoft Corporation in the United States and/or other countries. Netscape is a registered trademark of Netscape Communications Cor- Manager of Editorial Services: poration in the U.S. and other countries. Heather Talbot PHP 5 is copyright ©2001-2004 The PHP Group. MySQL is a registered Acquisitions Editor: trademark of MySQL AB in the United States, the European Union and Mitzi Koontz other countries. All other trademarks are the property of their respective owners. Senior Editor: Mark Garvey Important:Thomson Course Technology PTR cannot provide software support. Please contact the appropriate software manufacturer’s Associate Marketting Managers: technical support line or Web site for assistance. Kristin Eisenzopf and Thomson Course Technology PTR and the author have attempted through- Sarah Dubois out this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. Project Editor: Scott Harris/Argosy Publishing Information contained in this book has been obtained by Thomson Course Technology PTR from sources believed to be reliable. However, Technical Reviewer: because of the possibility of human or mechanical error by our Arlie Hartman sources, Thomson Course Technology PTR, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any Thomson Course Technology information and is not responsible for any errors or omissions or the PTR Market Coordinaator: results obtained from use of such information. Readers should be par- Amanda Weaver ticularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press. Copy Editor: Educational facilities, companies, and organizations interested in mul- Tonya Cupp tiple copies or licensing of this book should contact the publisher for quantity discount information. Training manuals, CD-ROMs, and por- Interior Layout Tech: tions of this book are also available individually or can be tailored for Shawn Morningstar specific needs. Cover Designer: ISBN: 1-59200-494-6 Mike Tanamachi Library of Congress Catalog Card Number: 2004108011 Printed in the United States of America CD-ROM Producer: 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1 Arlie Hartman Indexer: Maureen Shepherd Proofreadeer: Jan Cocker Thomson Course Technology PTR, a division of Thomson Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com To Heather, Elizabeth, Matthew, and Jacob, and to all those who have called me Teacher. A k o e gm n s c n wl d e t F irst I thank Him from whom all flows. Heather, you always work harder on these books than I do. Thank you for your love and your support. Thank you Elizabeth, Matthew, and Jacob for understanding why Daddy was typing all the time. Thanks to the Open Source community for creating great free software like PHP and MySQL. Also, thanks to the phpMyAdmin team and the SQLite team for developing such terrific software and making it freely available. Thank you, Stacy Hiquet, for your continued support and encouragement on this and other projects. Thanks, Scott Harris. You did a great job of juggling all those balls around. Thanks to Tonya Cupp for drastically improving the readability of the manuscript. Arlie Hartman, thank you for technical editing and for putting together the CD-ROM. Thanks to J. Wynia (www.phpgeek.com) for technical editing. Thanks also to Jason for use of PHPTriad on the CD-ROM. Special thanks to those who worked on the first edition. Your hard work is the foundation for something even better. Thank you to the many members of the Premier/Course team who worked on this book. A hugethanks goes to my CSCI N399 and N452 Server-Side Web Development classes and the many people who sent in comments and advice from the first edi- tion. Thank you for being patient with my manuscript, for helping me spot many errors, and for providing invaluable advice. I learned as much from you as you did from me. A o t t A t o b u he u h r A ndy Harrisbegan his teaching career as a high-school special education teacher. During that time, he taught himself enough computing to do part-time computer consulting and database work. He began teaching computing at the university level in the late 1980s as a part-time job. Since 1995 he has been a full-time lecturer in the Computer Science Department of Indiana University/Purdue University–Indianapolis, where he manages the Streaming Media Lab and teaches classes in several programming languages. His primary interests are PHP, Java, Microsoft languages, Perl, JavaScript, Web Data, virtual reality, portable devices, and streaming media. He has written numerous books on these and other technology topics. This page intentionally left blank C t n s t on e t a a G a c l n e Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter 1: Exploring the PHP Environment . . . . . . . . . . .1 Chapter 2: Using Variables and Input . . . . . . . . . . . . . . 21 Chapter 3: Controlling Your Code with Conditions and Functions . . . . . . . . . . . . . . .55 Chapter 4: Loops and Arrays . . . . . . . . . . . . . . . . . . . . .95 Chapter 5: Better Arrays and String Handling . . . . . .133 Chapter 6: Working with Files . . . . . . . . . . . . . . . . . . .181 Chapter 7: Writing Programs with Objects . . . . . . . . .229 Chapter 8: XML and Content Management Systems . .271 Chapter 9: Using MySQL to Create Databases . . . . . .299 Chapter 10: Connecting to Databases within PHP . . . .335 Chapter 11: Data Normalization . . . . . . . . . . . . . . . . . . .359 Chapter 12: Building a Three-Tiered Data Application . .383 Appendix A: Reviewing HTML and Cascading Style Sheets . . . . . . . . . . . . .on cd Appendix B: Using SQLite as an Alternative Data Source . . . . . . . . . . . . .on cd Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .429
Description: