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
� e following example contains the same echo statement you saw with the standard PHP script delimiters, but this time the statement is contained within a PHP
6
C H A P T E R 1 Getting Started with PHP
-Inlxl)PHP Code Blocks - Mozilla Firefox File Edit View History Bookmarks Tools Help
- GJ jj |L] | http://student200.ucb.sephone.us/PHP_Projects/Chapter.01/Chapter/PHPCodeBlocks.phfÿÿÿ""’"
This text is displayed using standard PHP script delimiters
| Done
Like the standard PHP script delimiters, the
3. Add the following echo statement (shown in bold) between the script delimiters:
4. Save the PHPCodeBlocks.php document, upload it to the Web server, and then open it from your Web server. Your Web browser should appear similar to Figure 1-2.
Figure 1-2 Output of a PHP script after adding a PHP script section
5. Close your Web browser window.
7
Creating PHP Code Blocks
-Inlxl)PHP Code Blocks - Mozilla Firefox File Edit View History Bookmarks Tools Help
* C? ju |G |http://student200.ucb.sephone.us/PHP_Projects/Chapter.01/Chapter/PHPCodeBlocks.php *
This text is displayed using standard PHP script delimiters
This text is displayedusing a PHP script section.
| Done
Short PHP Script Delimiters A simplifi ed method of writing PHP code declaration blocks is to use the short and ?> script delimiters. Short PHP script delimiters are similar to standard PHP script delimiters, except they do not include ‘php’ in the opening delimiter. � e syntax for short PHP script delim- iters is as follows: statements; ?>
� e following example shows how to use short delimiters with the echo statement you saw earlier: echo "Explore Africa!"; ?>
Unlike the script delimiters and the