PHP/MySQL Programming for the Absolute Beginner ISBN:1931841322 by Andy Harris Premier Press © 2003 (414 pages) With this guide, you will acquire skills necessary for practical programming applications and will learn how these skills can be put to use in real world scenarios and apply them to the next programming language you tackle CD Content Table of Contents PHP/MySQL Programming for the Absolute Beginner Introduction Exploring the PHP Chapter 1 Environment Chapter 2 - Using Variables and Input Controlling Your Code Chapter 3 - with Conditions and Functions Loops and Arrays: The Chapter 4 Poker Dice Game Better Arrays and String Chapter 5 Handling Chapter 6 - Working with Files Using MySQL to Create Chapter 7 Databases Connecting to Databases Chapter 8 Within PHP Chapter 9 - Data Normalization Building a Three-Tiered Chapter 10 Data Application Index List of Figures List of Tables List of In The Real World List of Sidebars CD Content Back Cover If you are new to programming with PHP and MySQL and are looking for a solid introduction, this is the book for you Developed by computer science instructors, books in the For the Absolute Beginner series teach the principles of programming through simple game creation You will acquire the skills that you need for more practical programming applications and will learn how these skills can be put to use in real-world scenarios Best of all, by the time you finish this book you will be able to apply the basic principles you’ve learned to the next programming language you tackle With the instructions in this book, you’ll learn to: Use MySQL to create databases Master variables and input Connect to databases within PHP Control your code with conditions and functions Build a three-tiered data application About the Author Andy Harris began his teach career as a high school special education teacher He began teaching at the university level in the late 1980s as a part-time job Since 1995, he has been a full-time lecturer at the Computer Science Department of Indiana University/Purdue University—Indianapolis He manages the IUPUI Streaming Media Lab and teaches classes in several programming languages PHP/MySQL Programming for the Absolute Beginner ANDY HARRIS Copyright © 2003 by Premier Press, a division of Course Technology All rights reserved No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Premier Press, except for the inclusion of brief quotations in a review The Premier Press logo and related trade dress are trademarks of Premier Press and may not be used without written permission Microsoft, Windows, Internet Explorer, Notepad, VBScript, ActiveX, and FrontPage are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Netscape is a registered trademark of Netscape Communications Corporation in the U.S and other countries All other trademarks are the property of their respective owners Important: Premier Press cannot provide software support Please contact the appropriate software manufacturer's technical support line or Web site for assistance Premier Press and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer Information contained in this book has been obtained by Premier Press from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information Readers should be particularly aware of the fact that the Internet is an everchanging entity Some facts may have changed since this book went to press ISBN: 1-931841-32-2 Library of Congress Catalog Card Number: 2003104019 Printed in the United States of America 03 04 05 06 07 BH 10 9 8 7 6 5 4 3 2 1 Premier Press, a division of Course Technology 25 Thomson Place Boston, MA 02210 Publisher: Stacy L Hiquet Senior Marketing Manager: Martine Edwards Marketing Manager: Heather Hurley Manager of Editorial Services: Heather Talbot Associate Marketing Manager: Kristin Eisenzopf Acquisitions Editor: Todd Jensen Project Editor: Sandy Doell Technical Reviewer: Jason Wynia Retail Market Coordinator: Sarah Dubois Interior Layout: Danielle Foster Cover Designer: Mike Tanamachi CD-ROM Producer: Keith Davenport Indexer: Kelly Talbot Proofreader: Margaret Bauer To Heather, Elizabeth, Matthew, and Jacob Acknowledgments First I thank Him from whom all flows Heather, you always work harder on these books than I do Thank you for your love and your support Thank you Elizabeth, Matthew, and Jacob for understanding why Daddy was typing all the time Thanks to the Open Source community for creating great free software like PHP and MySQL Thank you, Stacy Hiquet, for your continued support and encouragement on this and other projects Thanks, Todd Jensen, for holding this thing together Special thanks to Sandy Doell for turning my drivel into something readable Thanks to J Wynia (www.phpgeek.com) for technical editing Thanks also to Jason for use of PHPTriad on the CD-ROM Thank you to the webyog development team (http://www.webyog.com/sqlyog/) for use of the SQLyog tool A big thanks to Keith Davenport for putting together the CD-ROM It's a big job, and you did it well Thank you to the many members of the Premier/Course team who worked on this book A huge thanks to my CSCI N399 Server Side Web Development class in Spring 2003 Thank you for being patient with my manuscript, for helping me spot many errors, and for providing invaluable advice I learned as much from you as you did from me About the Author Andy Harris began his teaching career as a high school special education teacher During that time, he taught himself enough computing to do part-time computer consulting and database work He began teaching computing at the university level in the late 1980s as a part-time job Since 1995 he has been a full-time lecturer in the Computer Science Department of Indiana University/Purdue University—Indianapolis, where he manages the Streaming Media Lab and teaches classes in several programming languages His primary interests are Java, Microsoft languages, Perl, JavaScript, PHP, Web Data, virtual reality, portable devices, and streaming media Introduction If you've been watching the Web for a while you've probably noticed it is changing When the Web first entered into the public consciousness, it was a way to distribute documents These documents were pretty easy to make Anybody with a weekend and a text editor could get a Web page up and running Building a Web site in the early days was about making documents Today the Internet is much more than that Interesting Web sites are not simply documents; they are applications They have much more complexity and power You might think the Web is no longer a place for individuals or beginning programmers Many of the software development tools available are expensive and complicated To me, the most exciting thing about the Internet is its social implications There is a large community that believes in powerful, easy-to-use, free software That community has produced a number of exceptional programs, including PHP and MySQL PHP is a powerful programming language that lets you build dynamic Web sites It works well on a variety of platforms, and it's reasonably easy to understand MySQL is an impressive relational data management system used to build commercial quality databases PHP and MySQL are such powerful and easy-to-use platforms that they make Web programming accessible even for beginners In this book, I will teach you about programming Specifically, you will learn how to write programs on Web servers You'll learn all the main concepts of programming languages You'll also learn about how data works in the modern environment You'll learn commands and syntax, but you'll also learn the process of programming If you've never written a computer program before, this book will be a good introduction If you're an experienced programmer wanting to learn PHP and MySQL, you'll find this book to be a gentle introduction Programming is hard work, but it's also a lot of fun I had a great time Chapter 2: Using Variables and Input IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD Chapter 3: Controlling Your Code with Conditions and Functions IN THE REAL WORLD IN THE REAL WORLD Chapter 4: Loops and Arrays: The Poker Dice Game IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD Chapter 5: Better Arrays and String Handling IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD Chapter 6: Working with Files IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD Chapter 7: Using MySQL to Create Databases IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD Chapter 8: Connecting to Databases Within PHP IN THE REAL WORLD IN THE REAL WORLD Chapter 9: Data Normalization IN THE REAL WORLD IN THE REAL WORLD IN THE REAL WORLD List of Sidebars Chapter 1: Exploring the PHP Environment USING DIV AND SPAN ELEMENTS IN CSS Chapter 3: Controlling Your Code with Conditions and Functions ACQUIRING IMAGES CODE STYLE Chapter 7: Using MySQL to Create Databases ADVANTAGES OF SQL DETERMINING THE LENGTH OF A VARCHAR FIELD Chapter 9: Data Normalization THE TRUTH ABOUT INNER JOINS Chapter 10: Building a Three-Tiered Data Application WHY DID I STORE QUERIES IN THE DATABASE? CD Content Following are select files from this book's Companion CD-ROM These files are for your personal use, are copyright protected by the publisher, author, and/or other third parties Unauthorized use, reproduction, or distribution is strictly prohibited Click on the link(s) below to download the files to your computer: File Description PHP/MySQL Programming for the Absolute All CD Content Beginner Size 912,578 ... Developed by computer science instructors, books in the For the Absolute Beginner series teach the principles of programming through simple game creation You will acquire the skills that you need for more practical programming applications and will learn... to Jason for use of PHPTriad on the CD-ROM Thank you to the webyog development team (http://www.webyog.com/sqlyog/) for use of the SQLyog tool A big thanks to Keith Davenport for putting together the CD-ROM... of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information