Build Your own Database Driven Web Site Using PHP & MySQL is a practical hands-on guide to learning all the tools, principles and techniques needed to build a fully functional database-driven web site using PHP & MySQL. This book covers everything from installing PHP & MySQL under Windows, Linux, and Mac through to building a live web-based content management system.
SITEPOINT BOOKS Advocate best practice techniques Lead you through practical examples Provide working code for your web site Make learning easy and fun Detailed installation instructions Easy-to-understand diagrams Practical code examples WHAT’S INSIDE? PANTONE 2955 CPANTONE Orange 021 C CMYK 100, 45, 0, 37CMYK O, 53, 100, 0 Black 100%Black 50% CMYK: Pantone: Grey scale PANTONE 2955 CPANTONE Orange 021 C CMYK 100, 45, 0, 37CMYK O, 53, 100, 0 Black 100%Black 50% CMYK: Pantone: Grey scale Visit us on the Web at sitepoint.com or for sales and support email books@sitepoint.com ISBN: 978-0-9805768-1-8 ALL SOURCE CODE AVAILABLE FOR DOWNLOAD LEARNING PHP & MYSQL HAS NEVER BEEN SO EASY! Kevin Yank is a world-renowned leader in web development. When not writing best sellers, Kevin is the Technical Director of sitepoint.com and editor of the popular SitePoint Tech Times newsletter. Kevin has also co-authored Simply JavaScript and Everything You Know About CSS Is Wrong! ABOUT KEVIN YANK BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL TEACH YOURSELF PHP & MYSQL THE EASY WAY . Build Your Own Database Driven Web Site Using PHP & MySQL is a practical hands-on guide to learning all the tools, principles, and techniques needed to build a fully functional database driven web site using PHP & MySQL. This book covers everything from installing PHP and MySQL on Windows, Linux, and Mac computers through to building a live, web-based content management system. You’ll learn how to: Install PHP 5 & MySQL 5 on Windows, Linux, or Mac OS X Gain a thorough understanding of PHP syntax Master database design principles and SQL Build a working content management system Add, edit, and delete web content without using HTML Build an ecommerce shopping cart Utilize sessions and cookies to track site visitors Craft SEO-friendly and memorable URLs And a whole lot more . BY KEVIN YANK 4 TH EDITION PHP & MYSQL YANK BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL USD $39.95 WEB PROGRAMMING CAD $49.95 phpmysql4.indd 1 5/28/2009 5:51:24 PM Licensed to botuongxulang@yahoo.com www.it-ebooks.info Summary of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix 1. Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2. Introducing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3. Introducing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4. Publishing MySQL Data on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5. Relational Database Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 6. Structured PHP Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 7. A Content Management System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 8. Content Formatting with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . 241 9. Cookies, Sessions, and Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 10. MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 11. Advanced SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 12. Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 A. MySQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 B. MySQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 C. MySQL Column Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 D. PHP Functions for Working with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Licensed to botuongxulang@yahoo.com www.it-ebooks.info BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL BY KEVIN YANK 4TH EDITION Licensed to botuongxulang@yahoo.com www.it-ebooks.info Build Your Own Database Driven Web Site Using PHP & MySQL by Kevin Yank Copyright © 2009 SitePoint Pty. Ltd. Editor: Kelly SteeleManaging Editor: Chris Wyness Cover Design: Alex WalkerTechnical Editor: Andrew Tetlaw Indexer: Russell Brooks Latest Update: July 2009Printing History: 1st Ed. Aug. 2001, 2nd Ed. Feb. 2003, 3rd Ed. Oct. 2004 Fourth Edition: July 2009 Notice of Rights All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews. Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein. Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark. Published by SitePoint Pty. Ltd. 48 Cambridge Street Collingwood VIC Australia 3066. Web: www.sitepoint.com Email: business@sitepoint.com ISBN 978-0-9805768-1-8 Printed and bound in the United States of America iv Licensed to botuongxulang@yahoo.com www.it-ebooks.info About the Author As Technical Director for SitePoint, Kevin Yank keeps abreast of all that is new and exciting in web technology. Best known for the book you are reading right now, he also co-authored Simply JavaScript (http://www.sitepoint.com/books/javascript1/) with Cameron Adams and Everything You Know About CSS Is Wrong! (http://www.sitepoint.com/books/csswrong1/ ) with Rachel Andrew. In addition, Kevin hosts the SitePoint Podcast (http://www.sitepoint.com/podcast/) and writes the SitePoint Tech Times, a free email newsletter that goes out to over 240,000 subscribers worldwide. Kevin lives in Melbourne, Australia and enjoys speaking at conferences, as well as visiting friends and family in Canada. He’s also passionate about performing improvised comedy theater with Impro Melbourne (http://www.impromelbourne.com.au/) and flying light aircraft. Kevin’s personal blog is Yes, I’m Canadian (http://yesimcanadian.com/). About the Technical Editor Andrew Tetlaw has been tinkering with web sites as a web developer since 1997. At SitePoint he is dedicated to making the world a better place through the technical editing of SitePoint books, kits, articles, and newsletters. He is also a busy father of five, enjoys coffee, and often neglects his blog at http://tetlaw.id.au/. About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for Web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums. v Licensed to botuongxulang@yahoo.com www.it-ebooks.info Licensed to botuongxulang@yahoo.com www.it-ebooks.info To my parents, Cheryl and Richard, for making all this possible. Licensed to botuongxulang@yahoo.com www.it-ebooks.info Licensed to botuongxulang@yahoo.com www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Where to Find Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv The SitePoint Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv The Book’s Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv The SitePoint Newsletters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Your Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvi Tips, Notes, and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Chapter 1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Your Own Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Windows Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 All-in-one Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Installing Individual Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Mac OS X Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 All-in-one Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Installing Individual Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Linux Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Installing PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Post-Installation Set-up Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 What to Ask Your Web Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Your First PHP Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Full Toolbox, Dirty Hands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Licensed to botuongxulang@yahoo.com www.it-ebooks.info Chapter 2 Introducing MySQL . . . . . . . . . . . . . . . . . . . . . 53 An Introduction to Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Logging On to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Structured Query Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Creating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Inserting Data into a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Viewing Stored Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Modifying Stored Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Deleting Stored Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Let PHP Do the Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Chapter 3 Introducing PHP . . . . . . . . . . . . . . . . . . . . . . . . . 73 Basic Syntax and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Variables, Operators, and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 User Interaction and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Hiding the Seams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Avoid Advertising Your Technology Choices . . . . . . . . . . . . . . . . . . 104 Use PHP Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Many Templates, One Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Bring On the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 4 Publishing MySQL Data on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Connecting to MySQL with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Sending SQL Queries with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 x Licensed to botuongxulang@yahoo.com www.it-ebooks.info . BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL TEACH YOURSELF PHP & MYSQL THE EASY WAY ... Build Your Own Database Driven Web Site Using. YANK 4 TH EDITION PHP & MYSQL YANK BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL USD $39.95 WEB PROGRAMMING CAD $49.95 phpmysql4.indd