Build Your Own Database Driven Web Site Using PHP & Mysql
Total Page:16
File Type:pdf, Size:1020Kb
ALL SOURCE CODE AVAILABLE FOR DOWNLOAD Grey scale Grey CMYK: Pantone: Pantone: Black 50% CMYK O, 53, 100, 0 Orange 021 C PANTONE CMYK: TEACH YOURSELF PHP & MYSQL Black 100% CMYK 100, 45, 0, 37 2955 C PANTONE THE EASY WAY ... PHP & MYSQL & PHP BUILDGrey scaleYOUR OWN Build Your Own Database Driven Web Site Using PHP & MySQL is a practical hands-on guide to learning all the tools, principles, WHAT’S INSIDE? and techniques needed to build a fully functional database driven web site using PHP & MySQL. This book covers everything from DATABASEPANTONE Orange 021 C PANTONE 2955 C installing PHP and MySQL on Windows, Linux, and Mac computers through to building a live, web-based content management system. CMYK O, 53, 100, 0 CMYK 100, 45, 0, 37 Black 50% Black 100% You’ll learn how to: DRIVEN WEB SITE Install PHP 5 & MySQL 5 on Windows, Linux, or Mac OS X Gain a thorough understanding of PHP syntax Detailed installation instructions USING PHP & MYSQL Master database design principles and SQL BY KEVIN YANK Build a working content management system 4TH EDITION Add, edit, and delete web content without using HTML Build an ecommerce shopping cart DATABASE DRIVEN WEB SITE WEB DRIVEN DATABASE Utilize sessions and cookies to track site visitors Craft SEO-friendly and memorable URLs Easy-to-understand diagrams And a whole lot more ... ABOUT KEVIN YANK 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 BUILD YOUR OWN BUILD YOUR Simply JavaScript and Everything You Know About CSS Is Wrong! Practical code examples USING PHP & MYSQL SITEPOINT BOOKS WEB PROGRAMMING ISBN: 978-0-9805768-1-8 Advocate best practice techniques Lead you through practical examples Provide working code for your web site Make learning easy and fun LEARNING PHP & MYSQL HAS NEVER BEEN SO EASY! USD $39.95 CAD $49.95 Visit us on the Web at sitepoint.com or for sales and support email [email protected] YANK phpmysql4.indd 1 5/28/2009 5:51:24 PM 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 BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL BY KEVIN YANK 4TH EDITION iv Build Your Own Database Driven Web Site Using PHP & MySQL by Kevin Yank Copyright © 2009 SitePoint Pty. Ltd. Managing Editor: Chris Wyness Editor: Kelly Steele Technical Editor: Andrew Tetlaw Cover Design: Alex Walker Indexer: Russell Brooks Printing History: Latest Update: July 2009 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: [email protected] ISBN 978-0-9805768-1-8 Printed and bound in the United States of America v 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. To my parents, Cheryl and Richard, for making all this possible. 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 x 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 . ..