1 flum m ..
PHP 1m PROGRAMMINGM
km ml. WITH MySQL
// SECOND EDITION U '
V :DON GOSSELIN §
:DIANA KOKOSKA l :R0BERT EASTERBROOKS I
‘ÿIrfjif
. \ , . *\ \ \ i
PHP PROGRAMMING WITH MYSQL
PHP PROGRAMMING
WITH MYSQL
D O N G O S S E L I N , D I A N A K O K O S K A ,
R O B E R T E A S T E R B R O O K S
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
S E C O N D E D I T I O N
; COURSE TECHNOLOGY *« CENGAGE Learning"
© 2011 Course Technology, 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.
Library of Congress Control Number: 2009940005
ISBN-13: 978-0-5387-4584-0 ISBN-10: 0-5387-4584-3
Course Technology 20 Channel Center Street Boston, MA 02210 USA
Cengage Learning is a leading provider of customized learning solutions with offi ce locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local offi ce at: www.cengage.com/global
Cengage Learning products are represented in Canada by Nelson Education, Ltd.
To learn more about Course Technology, visit www.cengage.com/coursetechnology
Purchase any of our products at your local college store or at our preferred online store www.CengageBrain.com
Some of the product names and company names used in this book have been used for identifi cation purposes only and may be trademarks or regis- tered trademarks of their respective manufacturers and sellers.
Course Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice.
PHP Programming with MySQL, Second Edition Don Gosselin, Diana Kokoska, Robert Easterbrooks
Executive Editor: Marie Lee
Acquisitions Editor: Amy Jollymore
Managing Editor: Tricia Coia
Senior Product Manager: Alyssa Pratt
Developmental Editor: Dan Seiter
Content Project Manager: Jennifer Feltri
Editorial Assistant: Zina Kresin
Art Director: Marissa Falco
Text Designer: Shawn Girsberger
Cover Designer: Cabbage Design Company
Cover Image: © CSA Images
Print Buyer: Julio Esperas
Copy Editor: Camille Kiolbasa
Proofreader: Andrea Schein
Indexer: Alexandra Nickerson
Compositor: Integra
For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706
For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions
Further permissions questions can be e-mailed to permissionrequest@cengage.com
Printed in the United States of America 1 2 3 4 5 6 7 13 12 11 10
* COURSE TECHNOLOGY CENGAGE Learning"
Brief Contents
Preface . . . . . . . . . . . . . . . . . xiv
CHAPTER 1 Gett ing Star ted wi th PHP . . . . . . . . . . 1
CHAPTER 2 Funct ions and Contro l Structures . . . . . . 74
CHAPTER 3 Manipulat ing Str ings . . . . . . . . . . . 124
CHAPTER 4 Handl ing User Input . . . . . . . . . . . 188
CHAPTER 5 Work ing wi th F i les and Director ies . . . . . 233
CHAPTER 6 Manipulat ing Arrays . . . . . . . . . . . 298
CHAPTER 7 Work ing wi th Databases and MySQL . . . . 380
CHAPTER 8 Manipulat ing MySQL Databases with PHP . . 446
CHAPTER 9 Managing State Informat ion . . . . . . . . 496
CHAPTER 10 Developing Object -Or iented PHP . . . . . . 556
APPENDIX A Work ing wi th XHTML . . . . . . . . . . . 620
APPENDIX B Conf igur ing a Personal Web Server . . . . 636
APPENDIX C Formatt ing Str ings. . . . . . . . . . . . 641
APPENDIX D Secure Coding with PHP . . . . . . . . . 648
APPENDIX E Error Handl ing and Debugging. . . . . . . 655
APPENDIX F Connect ing to SQL Server and Oracle Databases . . . . . . . . . . 671
Index . . . . . . . . . . . . . . . . . 679
v
Contents
Preface . . . . . . . . . . . . . . . . . xiv
CHAPTER 1 Gett ing Star ted wi th PHP . . . . . . . . . . 1
Creating Basic PHP Scripts . . . . . . . . . . . . . . . . . 2 Creating PHP Code Blocks . . . . . . . . . . . . . . . . . 3
Standard PHP Script Delimiters . . . . . . . . . . . . . . 3 The