Download php and mysql 2017 easy to learn and more Slides System Analysis and Design in PDF only on Docsity!
ANDY HARRIS
PHP 5/MySQL
Programming
© 2004 by Thomson Course Technology PTR. 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, record- ing, or by any information storage or retrieval system without written permission from Thomson Course Technology PTR, except for the inclusion of brief quotations in a review.
The Thomson Course Technology PTR logo and related trade dress are trademarks of Thomson Course Technology PTR 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 Cor- poration in the U.S. and other countries.
PHP 5 is copyright © 2001-2004 The PHP Group. MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries.
All other trademarks are the property of their respective owners.
Important: Thomson Course Technology PTR cannot provide software support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance.
Thomson Course Technology PTR and the author have attempted through- out 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 Thomson Course Technology PTR from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Thomson Course Technology PTR, 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 par- ticularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press.
Educational facilities, companies, and organizations interested in mul- tiple copies or licensing of this book should contact the publisher for quantity discount information. Training manuals, CD-ROMs, and por- tions of this book are also available individually or can be tailored for specific needs.
ISBN: 1-59200-494- Library of Congress Catalog Card Number: 2004108011 Printed in the United States of America 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1
Thomson Course Technology PTR, a division of Thomson Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com
SVP, Thomson Course Technology PTR: Andy Shafran Publisher: Stacy L. Hiquet Senior Marketing Manager: Sarah O’Donnell Marketiing Manager: Heather Hurley Manager of Editorial Services: Heather Talbot Acquisitions Editor: Mitzi Koontz Senior Editor: Mark Garvey Associate Marketting Managers: Kristin Eisenzopf and Sarah Dubois Project Editor: Scott Harris/Argosy Publishing Technical Reviewer: Arlie Hartman Thomson Course Technology PTR Market Coordinaator: Amanda Weaver Copy Editor: Tonya Cupp Interior Layout Tech: Shawn Morningstar Cover Designer: Mike Tanamachi CD-ROM Producer: Arlie Hartman Indexer: Maureen Shepherd Proofreadeer: Jan Cocker
irst 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. Also, thanks to the phpMyAdmin team and the SQLite team for
developing such terrific software and making it freely available.
Thank you, Stacy Hiquet, for your continued support and encouragement on this
and other projects.
Thanks, Scott Harris. You did a great job of juggling all those balls around.
Thanks to Tonya Cupp for drastically improving the readability of the manuscript.
Arlie Hartman, thank you for technical editing and for putting together the CD-ROM.
Thanks to J. Wynia (www.phpgeek.com) for technical editing. Thanks also to Jason
for use of PHPTriad on the CD-ROM.
Special thanks to those who worked on the first edition. Your hard work is the
foundation for something even better.
Thank you to the many members of the Premier/Course team who worked on
this book.
Ahuge thanks goes to my CSCI N399 and N452 Server-Side Web Development
classes and the many people who sent in comments and advice from the first edi-
tion. 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.
Acknowledgments
F
ndy 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 PHP, Java, Microsoft languages, Perl, JavaScript, Web Data,
virtual reality, portable devices, and streaming media. He has written numerous
books on these and other technology topics.
About the Author
A
Contents at a
Glance
Introduction....................................... xxi
Chapter 1: Exploring the PHP Environment...........
Chapter 2: Using Variables and Input.............. 21
Chapter 3: Controlling Your Code with Conditions and Functions...............
Chapter 4: Loops and Arrays.....................
Chapter 5: Better Arrays and String Handling......
Chapter 6: Working with Files...................
Chapter 7: Writing Programs with Objects.........
Chapter 8: XML and Content Management Systems..
Chapter 9: Using MySQL to Create Databases......
Chapter 10: Connecting to Databases within PHP....
Chapter 11: Data Normalization...................
Chapter 12: Building a Three-Tiered Data Application..
Appendix A: Reviewing HTML and Cascading Style Sheets............ .on cd
Appendix B: Using SQLite as an Alternative Data Source............ .on cd
Index...............................
Introduction....................................... xxi
xii
xiii
xv
xvi
xviii
xix
- Chapter 1: Exploring the PHP Environment.
- Introducing the Tip of the Day Program.
- Programming on the Web Server
- Installing PHP and Apache.
- Using an Existing Server
- Installing Your Own Development Environment
- Installing Apache
- Installing Apache Files
- Testing Your Server.
- Starting Apache as a Service
- Configuring Apache
- Running Your Local Server
- Installing PHP
- Downloading the PHP Program
- Telling Apache about PHP
- Adding PHP to Your Pages
- Adding PHP Commands to an HTML Page
- Examining the Results
- Configuring Your Version of PHP
- Safe Mode
- Register Globals
- Windows Extensions.
- Creating the Tip of the Day Program
- Summary.
- Chapter 2: Using Variables and Input.
- Introducing the Story Program
- Using Variables in Your Scripts.
- Introducing the Hi Jacob Program
- Creating a String Variable
- Printing a Variable’s Value
- Using the Semicolon to End a Line.
- Using Variables for More-Complex Pages
- Building the Row Your Boat Page
- Creating Multi-Line Strings
- Working with Numeric Variables
- Making the ThreePlusFive Program
- Assigning Numeric Values
- Using Mathematical Operators
- Creating a Form to Ask a Question.
- Building an HTML Page with a Form
- Setting the Action Attribute to a Script File
- Writing a Script to Retrieve the Data.
- Sending Data without a Form
- Understanding the get Method
- Using a URL to Embed Form Data
- Working with Multiple Field Queries.
- Reading Input from Other Form Elements.
- Introducing the borderMaker Program
- Building the borderMaker.html Page
- Reading the Form Elements.
- Returning to the Story Program
- Designing the Story
- Building the HTML Page.
- Checking the Form
- Building the Final Story
- Summary.
- Conditions and Functions. Chapter 3: Controlling Your Code with
- Examining the Petals Around the Rose Game
- Creating a Random Number
- Viewing the Roll Em Program
- Printing a Corresponding Image
- Using the if Statement to Control Program Flow
- Introducing the Ace Program.
- Creating a Condition
- Exploring Comparison Operators
- Creating an if Statement.
- Working with Negative Results
- Demonstrating the Ace or Not Program
- Using the else Clause
- Working with Multiple Values
- Writing the Binary Dice Program.
- Using Multiple else if Clauses
- Using the switch Structure to Simplify Programming
- Building the Switch Dice Program
- Using the switch Structure.
- Combining a Form and Its Results
- Responding to Checkboxes
- Using Functions to Encapsulate Parts of the Program
- Examining the This Old Man Program
- Creating New Functions
- Using Parameters and Function Values
- Examining the Param.php Program
- Looking at Encapsulation in the Main Code Body
- Returning a Value: The chorus() Function
- Accepting a Parameter in the verse() Function
- Managing Variable Scope
- Looking at the Scope Demo.
- Returning to the Petals Game
- Starting HTML
- Main Body Code
- The printGreeting() Function
- The printDice() Function
- The showDie() Function
- The calcNumPetals Function
- The printForm() Function
- The Ending HTML Code
- Summary.
- Chapter 4: Loops and Arrays.
- Introducing the Poker Dice Program
- Counting with the for Loop
- Initializing a Sentry Variable.
- Setting a Condition to Finish the Loop
- Changing the Sentry Variable
- Building the Loop
- Modifying the for Loop
- Counting by Fives
- Counting Backwards
- Using a while Loop.
- Repeating Code with a while Loop
- Recognizing Endless Loops
- Building a Well-Behaved Loop.
- Working with Basic Arrays
- Generating a Basic Array
- Using a Loop to Examine an Array’s Contents
- Using the array() Function to Preload an Array
- Detecting the Size of an Array
- Improving This Old Man with Arrays and Loops
- Building the Place Array
- Writing Out the Lyrics
- Keeping Persistent Data.
- Counting with Form Fields
- Storing Data in the Text Box
- Using a Hidden Field for Persistence
- Writing the Poker Dice Program
- Setting Up the HTML
- Building the Main Code Body
- Making the rollDice() Function.
- Creating the evaluate() Function
- Printing the Results.
- Summary.
- Chapter 5: Better Arrays and String Handling.
- Introducing the Word Search Program Creator.
- Using the foreach Loop to Work with an Array.
- Introducing the foreach.php Program.
- Creating an Associative Array
- Examining the assoc.php Program
- Building an Associative Array
- with the array() Function Building an Associative Array
- Using foreach with Associative Arrays
- Using Built-In Associative Arrays
- Introducing the formReader.php Program
- Reading the $_REQUEST Array
- Creating a Multidimensional Array. - Multidimensional Array Building the HTML for the Basic
- Responding to the Distance Query
- Making a Two-Dimensional Associative Array
- Building the HTML for the Associative Array.
- Responding to the Query
- Building the Two-Dimensional Associative Array
- Associative Array Getting Data from the Two-Dimensional
- Manipulating String Values - the Pig Latin Translator Demonstrating String Manipulation with
- Building the Form
- String into an Array Using the split() Function to Break a
- Trimming a String with rtrim().
- Finding a Substring with substr()
- String Inside Another Using strstr() to Search for One
- Using the Concatenation Operator
- Finishing the Pig Latin Program
- Translating Between Characters and ASCII Values
- Returning to the Word Search Creator.
- Getting the Puzzle Data from the User
- Setting Up the Response Page
- Working with the Empty Data Set
- Building the Program’s Main Logic
- Parsing the Word List
- Clearing the Board
- Filling the Board
- Adding a Word
- Making a Puzzle Board
- Adding the Foil Letters
- Printing the Puzzle.
- Printing the Answer Key.
- Summary.
- Chapter 6: Working with Files.
- Previewing the Quiz Machine
- Entering the Quiz Machine System
- Editing a Quiz
- Taking a Quiz
- Seeing the Results
- Viewing the Quiz Log
- Saving a File to the File System
- Introducing the saveSonnet.php Program
- Opening a File with fopen()
- Creating a File Handle.
- Examining File Access Modifiers.
- Writing to a File
- Closing a File
- Loading a File from the Drive System
- Introducing the loadSonnet.php Program
- Beautifying Output with CSS
- Using the “r” Access Modifier.
- Checking for the End of the File with feof()
- Reading Data from the File with fgets()
- Reading a File into an Array
- Introducing the cartoonifier.php Program
- Loading the File into an Array with file()
- Using str_replace() to Modify File Contents
- Working with Directory Information
- Introducing the imageIndex.php Program
- Creating a Directory Handle with openDir()
- Getting a List of Files with readdir()
- Selecting Particular Files with preg_grep().
- Using Basic Regular Expressions
- Storing the Output
- Working with Formatted Text
- Introducing the mailMerge.php Program
- Determining a Data Format
- Examining the mailMerge.php Code.
- Loading Data with the file() Command
- Splitting a Line into an Array and to Scalar Values
- Creating the QuizMachine.php Program
- Building the QuizMachine.php Control Page
- Editing a Test
- Writing the Test
- Taking a Quiz
- Grading the Quiz
- Viewing the Log
- Summary.
- Chapter 7: Writing Programs with Objects.
- Introducing the SuperHTML Object
- Building a Simple Document with SuperHTML
- Working with the Title Property
- Adding Text and Tags with SuperHTML
- Creating Lists the SuperHTML Way
- Making Tables with SuperHTML
- Creating Super Forms.
- Understanding OOP
- Objects Overview
- Creating a Basic Object.
- Adding Methods to a Class
- Inheriting from a Parent Class
- Building the SuperHTML Class
- Setting Up the File
- Creating the Constructor
- Manipulating Properties
- Adding Text
- Building the Top of the Page
- Creating the Bottom of the Page
- Adding Headers and Generic Tags
- Creating Lists from Arrays
- Creating Tables from 2-Dimension Arrays
- Creating Tables One Row at a Time
- Building Basic Form Objects
- Building Select Objects.
- Responding to Form Input
- Summary.
- Management Systems. Chapter 8: XML and Content
- Introducing XCMS
- Understanding Content Management Systems
- Working with PHP-Nuke
- Installing PHP-Nuke.
- Customizing PHP-Nuke
- Introducing simpleCMS
- Viewing Pages from a User’s Perspective
- Examining the PHP Code
- Viewing the CSS
- Inspecting the Menu System
- Improving the CMS with XML.
- Introducing XML
- Examining main.xml
- Simplifying the Menu Pages
- Introducing XML Parsers
- Working with Simple XML
- Working with the simpleXML API
- with the simpleXML API. Manipulating More-Complex XML
- Returning to XCMS
- Extracting Data from the XML File
- Summary.
- Chapter 9: Using MySQL to Create Databases.
- Introducing the Adventure Generator Program
- Using a Database Management System
- Working with MySQL
- Installing MySQL
- Using the MySQL Executable
- Creating a Database
- Creating a Table.
- Inserting Values
- Selecting Results
- Writing a Script to Build a Table
- Creating Comments in SQL
- Dropping a Table
- Running a Script with SOURCE
- Working with a Database via phpMyAdmin
- Connecting to a Server
- Creating and Modifying a Table.
- Editing Table Data
- Exporting a Table
- Creating More-Powerful Queries
- Limiting Columns
- Limiting Rows with the WHERE Clause
- Changing Data with the UPDATE Statement
- Returning to the Adventure Game
- Designing the Data Structure.
- Summary.
- within PHP. Chapter 10: Connecting to Databases
- Connecting to the Hero Database
- Getting a Connection
- Choosing a Database
- Creating a Query
- Getting Field Names
- Parsing the Result Set.
- Returning to the Adventure Game Program
- Connecting to the Adventure Database
- Displaying One Segment
- Viewing and Selecting Records
- Editing the Record.
- Committing Changes to the Database
- Summary.
- Chapter 11: Data Normalization.
- Introducing the spy Database
- The badSpy Database
- Inconsistent Data Problems
- Problem with the Operation Information
- Problems with Listed Fields
- Designing a Better Data Structure
- Defining Rules for a Good Data Design
- Normalizing Your Data
- Defining Relationship Types
- Building Your Data Tables
- Setting Up the System
- Creating the agent Table
- Building the operation Table
- Using a Join to Connect Tables
- Creating Useful Joins
- Examining a Join without a WHERE Clause.
- Adding a WHERE Clause to Make a Proper Join
- Adding a Condition to a Joined Query
- Building a Link Table for Many-to-Many Relationships
- Enhancing the ER Diagram
- Creating the specialty Table
- Interpreting the agent_specialty Table with a Query
- Creating Queries That Use Link Tables
- Summary.
- Data Application Chapter 12: Building a Three-Tiered
- Introducing the SpyMaster Program
- Viewing the Main Screen
- Viewing the Results of a Query
- Viewing Table Data
- Editing a Record
- Confirming the Record Update
- Deleting a Record
- Adding a Record
- Processing the Add
- Building the Design of the SpyMaster System
- Creating a State Diagram
- Designing the System.
- Building a Library of Functions
- Writing the Non-Library Code
- Preparing the Database
- Examining the spyMaster.php Program
- Building the viewQuery.php Program.
- Viewing the editTable.php Program
- Viewing the editRecord.php Program
- Viewing the updateRecord.php Program
- Viewing the deleteRecord.php Program
- Viewing the addRecord.php Program
- Viewing the processAdd.php Program
- Creating the spyLib Library Module
- Setting a CSS Style
- Setting Systemwide Variables
- Connecting to the Database
- Creating a Quick List from a Query
- Building an HTML Table from a Query
- Building an HTML Table for Editing an SQL Table.
- Creating a Generic Form to Edit a Record
- Building a Smarter Edit Form
- Determining the Field Type