Visual QuickPro Guide
PHP and MySQL for Dynamic Web Sites
Fourth Edition
Larry Ullman
Visual QuickPro Guide
PHP and MySQL for Dynamic Web Sites, Fourth Edition
Larry Ullman
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 (fax)
Find us on the Web at: www.peachpit.com
To report errors, please send a note to:
Peachpit Press is a division of Pearson Education.
Copyright 2012 by Larry Ullman
Editor: Rebecca Gulick
Copy Editor: Patricia Pane
Technical Reviewer: Anselm Bradford
Production Coordinator: Myrna Vladic
Compositor: Debbie Roberti
Proofreader: Bethany Stough
Indexer: Valerie Haynes-Perry
Cover Design: RHDG / Riezebos Holzbaur Design Group, Peachpit Press
Interior Design: Peachpit Press
Logo Design: MINE www.minesf.com
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact .
Notice of Liability
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it.
Trademarks
Visual QuickPro Guide is a registered trademark of Peachpit Press, a division of Pearson Education. MySQL is a registered trademark of MySQL AB in the United States and in other countries. Macintosh and Mac OS X are registered trademarks of Apple, Inc. Microsoft and Windows are registered trademarks of Microsoft Corp. Other product names used in this book may be trademarks of their own respective owners. Images of Web sites in this book are copyrighted by the original holders and are used with their kind permission. This book is not officially endorsed by nor affiliated with any of the above companies, including MySQL AB.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
ISBN-13: 978-0-321-78407-0
ISBN-10: 0-321-78407-3
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
Dedication
Dedicated to the fine faculty at my alma mater, Northeast Missouri State University. In particular, I would like to thank: Dr. Monica Barron, Dr. Dennis Leavens, Dr. Ed Tyler, and Dr. Cole Woodcox, whom I also have the pleasure of calling my friend. I would not be who I am as a writer, as a student, as a teacher, or as a person if it were not for the magnanimous, affecting, and brilliant instruction I received from these educators.
Special Thanks to
My heartfelt thanks to everyone at Peachpit Press, as always.
My gratitude to editor extraordinaire Rebecca Gulick, who makes my job so much easier. And thanks to Patricia Pane for her hard work, helpful suggestions, and impressive attention to detail. Thanks also to Valerie Haynes-Perry for indexing and Myrna Vladic and Deb Roberti for laying out the book, and thanks to Anselm Bradford for his technical review.
Kudos to the good people working on PHP, MySQL, Apache, phpMyAdmin, MAMP, and XAMPP, among other great projects. And a hearty cheers to the denizens of the various newsgroups, mailing lists, support forums, etc., who offer assistance and advice to those in need.
Thanks, as always, to the readers, whose support gives my job relevance. An extra helping of thanks to those who provided the translations in , and who offered up recommendations as to what theyd like to see in this edition.
Thanks to Karnesha and Sarah for entertaining and taking care of the kids so that I could get some work done.
Finally, I would not be able to get through a single book if it werent for the love and support of my wife, Jessica. And a special shout-out to Zoe and Sam, who give me reasons to, and not to, write books!
Introduction
Todays Web users expect exciting pages that are updated frequently and provide a customized experience. For them, Web sites are more like communities, to which theyll return time and again. At the same time, Web-site administrators want sites that are easier to update and maintain, understanding thats the only reasonable way to keep up with visitors expectations. For these reasons and more, PHP and MySQL have become the de facto standards for creating dynamic, database-driven Web sites.
This book represents the culmination of my many years of Web development experience coupled with the value of having written several previous books on the technologies discussed herein. The focus of this book is on covering the most important knowledge in the most efficient manner. It will teach you how to begin developing dynamic Web sites and give you plenty of example code to get you started. All you need to provide is an eagerness to learn.
Well, that and a computer.
What Are Dynamic Web Sites?
Dynamic Web sites are flexible and potent creatures, more accurately described as applications than merely sites. Dynamic Web sites
Respond to different parameters (for example, the time of day or the version of the visitors Web browser)
Have a memory, allowing for user registration and login, e-commerce, and similar processes
Almost always integrate HTML forms, allowing visitors to perform searches, provide feedback, and so forth
Often have interfaces where administrators can manage the sites content
Are easier to maintain, upgrade, and build upon than statically made sites
There are many technologies available for creating dynamic Web sites. The most common are ASP.NET (Active Server Pages, a Microsoft construct), JSP (Java Server Pages), ColdFusion, Ruby on Rails (a Web development framework for the Ruby programming language), and PHP. Dynamic Web sites dont always rely on a database, but more and more of them do, particularly as excellent database applications like MySQL are available at little to no cost.
What is PHP?
PHP originally stood for Personal Home Page as it was created in 1994 by Rasmus Lerdorf to track the visitors to his online rsum. As its usefulness and capabilities grew (and as it started being used in more professional situations), it came to mean PHP: Hypertext Preprocessor.
According to the official PHP Web site, found at www.php.net, PHP is a widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its a long but descriptive definition, whose meaning Ill explain.
Next page