TR A IN IN G & REFERENCE m M urach murach's and JOEL MURACH RAY HARRIS GET OFF TO A FAST START by learning how to develop, test, and debug a PHP application that gets data from a MySQL database in just chapters! MASTER PHP as you learn how to work with form data, dates, arrays, sessions, cookies, Amenons, objects, and regular expressions MASTER MySQL as you learn how to design a database, create a database, and use SQL and l’HP to work like a pro with a database MASTER ADVANCED WEB SKILLS like how to use secure connections, authenticate users, send email, access content from other web sites, upload tiles, process images, and more TRAINING & REFERENCE m u r a c h 's and MySQL Joel Murach Ray Harris M ik e M ura ch & A ssociates , I n c 1-800-221-5528 · (559) 440-9071 · Fax: (559) 440-0963 murachbooks@murach.com · www.murach.com Authors: Joel Murach Ray Harris Editor: Mike Murach Ben Murach Cover Design: Zylka Design Production: Cynthia Yasquez More books for web developers Murach ’s HTML, XHTML, and CSS Murach’s JavaScript and DOM Scripting Murach’s ASP.NET Web Programming with C# 2010 Murach’s ASP.NET Web Programming with VB 2010 Murach’s Java Servlets and JSP (Second Edition) Books for database programmers Murach’s Oracle SQL and PUSQL Murach ’s SQL Server 2008for Developers Murach’s ADO.NET Database Programming with C# 2010 Murach’s ADO.NET Database Programming with VB 2010 Books on Visual Basic, C#, and Java Murach ’s C# 2010 Murach’s Visual Basic 2010 Murach ’s Java SE Books for IBM mainframe programmers Murach’s OS/390 and z/OS JCL Murach ’s Mainframe COBOL Murach’s C1CS for the COBOL Programmer DB2 for the COBOL Programmer, Part For more on Murach books, please visit us at www.murach.com ©2010, Mike Murach & Associates, Inc All rights reserved Printed in the United States of America 10 ISBN-13: 978-1-890774-56-1 Contents Introduction Section Get started fast with PHP and MySQL Chapter Introduction to web development with PHP Chapter How to code a PHP application Chapter Introduction to relational databases and MySQL Chapter How to use PHP with a MySQL database Chapter How to use the MVC pattern to organize your code Chapter How to test and debug a PHP application Section Chapter Chapter Chapter Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 45 93 123 159 189 Master PHP programming How to work with form data 207 How to code control statements 231 How to work with strings and numbers 259 How to work with dates 291 How to create and use arrays 311 How to work with cookies and sessions 347 How to create and use functions 379 How to create and use objects 413 How to use regular expressions, handle exceptions, and validate data 461 Section Master MySQL programming Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 How to design a database How to use SQL to create a MySQL database How to use SQL to work with a MySQL database Professional PHP for working with MySQL A database-driven web site 503 537 573 611 645 Section Chapter 21 Chapter 22 Chapter 23 Chapter 24 Master the advanced skills for building web sites How to create secure web sites How to send email and access other web sites How to work with files, uploads, and images An eCommerce web site 675 707 743 779 Reference Aids Appendix A How to set up your PC for this book Appendix B How to set up your Mac for this book Index 801 817 832 Expanded contents Expanded contents Section Get started fast with PHP and MySQL Chapter Introduction to web development with PHP The architecture of a w eb a p p lic a tio n How a client-server architecture works How static web pages are processed How dynamic web pages are processed A survey of web application software 10 Highlights in the history of PHP 12 Highlights in the history of MySQL 12 The Product D iscoun t a p p lic a tio n 14 The user interface 14 The HTML f ile 16 The CSS file 18 The PHP f ile 20 How to ed it and te s t a PHP a p p lica tio n 22 How to edit a PHP page with a text editor .22 How to start and stop Apache and MySQL on your own computer 24 How to deploy a PHP application 26 How to run a PHP application 28 How to test and debug a PHP page 30 How to view the source code for a web p age 32 How to u se N etB ean s to d ev elo p a PHP a p p lic a tio n .34 How to work with PHP projects and files 34 How to edit and test a PHP application 36 How to import and configure a PHP project 38 Chapter How to code a PHP application B a sic PHP s k ills 46 How to embed PHP in HTML 46 How to code comments and statements 48 The six PHP data types 50 How to declare variables and constants 52 How to g et data from a r e q u e s t .54 How to use the built-in $_GET array 54 How to use the built-in $_POST array 56 When to use the HTTP GET and POST methods 56 How to work with d a ta 58 How to code string expressions 58 How to code echo statements 58 How to code numeric expressions 60 How to use the compound assignment operators 62 How to use some built-in functions 64 The Product D iscoun t a p p lic a tio n 66 The user interface .66 The form in the HTML file 66 The PHP f ile 68 Expanded contents How to c o d e control s t a te m e n t s 70 How to code conditional expressions .70 How to code if statements 72 How to code while and for statements 74 How to pass control to another page 76 The Future Value a p p lica tio n 78 The user interface 78 The code for the index.php file 80 The code for the display_results.php file 82 How to u s e th e PHP d o cu m e n ta tio n 86 How to access the PHP manual 86 How to find the documentation you need 86 Chapter Introduction to relational databases with MySQL An introduction to relational d a ta b a s e s 94 How a database table is organized 94 How the tables in a relational database are related 96 How the columns in a table are defined 98 The SQL sta tem en ts for data m a n ip u la tio n .100 How to select data from a single table 100 How to select data from multiple tables 102 How to insert, update, and delete data 104 An introduction to M ySQL 106 What MySQL provides 106 Two ways to work with MySQL 108 How to u s e phpM yAdm in 110 How to start phpMyAdmin 110 How to log in, log out, and change your password 110 How to import and run a SQL script that creates a database 112 How to review the data and structure of a table 114 How to run SQL statements 116 How to create users with limited privileges 118 Chapter How to use PHP with a MySQL database PHP for working with M ySQ L 124 How to connect to a MySQL database 124 How to execute SELECT statements 126 How to execute INSERT, UPDATE, and DELETE statements 128 How to use a try/catch statement to handle exceptions 130 How to g et data from a result s e t 132 How to work with arrays 132 How to get the data from the first row of a result s e t 134 How to get the data from all the rows of a result s e t 136 The Product Viewer a p p lic a tio n 138 The user interface 138 The co d e .140 The Product Manager a p p lica tio n 146 The user interface 146 The co d e 148 Expanded contents Chapter How to use the MVC pattern to organize your code How to u se th e MVC p attern 160 An introduction to the MVC pattern 160 How to code functions 162 How to redirect requests 164 The Product Manager a p p lica tio n 166 The user interface 166 The m odel 168 The controller 170 The v ie w 172 The Product C atalog a p p lic a tio n 178 The user interface 178 The m odel 180 The controller 180 The v ie w 182 Chapter How to test and debug a PHP application An introduction to testin g and d e b u g g in g .190 Typical test phases for a PHP application 190 The three types of errors that can occur 190 Common PHP errors 192 An easy way to trace the execution of your PHP code 194 How to d eb u g with xD ebug and N e t B e a n s 196 How to set and remove breakpoints 196 How to step through cod e 198 How to inspect variables 198 How to inspect the stack trace 200 S e c t io n M a s te r P H P p ro g m m in g Chapter How to work with form data How to get data from a form 208 How to get data from text boxes, password boxes, and hidden fields .208 How to get data from a radio button 210 How to get data from a check box 212 How to get data from an array of check boxes 214 How to get data from a drop-down list 216 How to get data from a list b o x 218 How to get data from a text area 220 How to display data on a web page 222 How to format special characters 222 How to format line breaks 224 How to display data with echo and print statements 226 Chapter How to code control statements How to code conditional expressions 232 How to use the equality and identity operators 232 How to use the relational operators 234 How to use the logical operators 236 Expanded contents How to code the selection structures 238 How to code if statements with else clauses 238 How to code if statements with else if clauses 240 How to use the conditional operator 242 How to code switch statements .244 How to use a switch statement in the controller 246 How to code the iteration structures 248 How to code while loo p s 248 How to code do-while loops 250 How to code for loops 252 How to use the break and continue statements .254 Chapter How to work with strings and numbers How to work with strings 260 How to create strings 260 How to use escape sequences 262 How to work with string length and substrings 264 How to search a string 266 How to replace part of a string .266 How to modify strings 268 How to convert strings to and from arrays .270 How to convert strings to and from ASCII values 270 How to compare strings 272 How to work with numbers .274 How to assign integers .274 How to assign floating-point values 276 How to use the math functions .278 How to generate random numbers 280 Other skills for working with stringsand numbers 282 How to use the sprintf function to format strings and numbers 282 How to convert strings to numbers 286 Chapter 10 How to work with dates How to use timestamps to work with dates 292 How to create and format timestamps 292 How to work with timestamps 294 How to use the strtotime function 296 Examples of working with timestamps 298 How to use objects to work with dates 300 How to use the DateTime class .300 How to use the Datelnterval class 302 How to use the Datelnterval and DateTimeclasses together .304 Examples of working with DateTime objects 306 Chapter 11 How to create and use arrays How to create and use an array 312 How to create an array .312 How to add and delete elements .314 How to work with variable substitution 314 How to use for loops to work with arrays 316 VÏÏ viii Expanded contents How to create and u se an a sso c ia tiv e a rra y 318 How to create an associative array 318 How to add and delete elements 320 How to work with variable substitution 320 How to use foreach loops to work with arrays 322 How to u s e fu n ctio n s to work with a rrays 324 How to fill, merge, slice, and splice arrays 324 How to work with queues and stacks 326 How to get the sum and product of elem ents 328 How to search arrays 328 How to sort arrays 330 How to modify arrays .332 How to work with arrays o f a r r a y s 334 Understanding an array of arrays 334 How to create and use an array of arrays 336 The Task List Manager a p p lic a tio n 338 The user interface 338 The code for the controller 338 The code for the v ie w 340 Chapter 12 How to work with cookies and sessions How to work with c o o k ie s 348 An introduction to cookies 348 How to set and get a cookie .350 How to enable or disable cookies 352 How to work with s e s s i o n s 354 Why session tracking is difficult with H TTP 354 How session tracking works in PHP 354 How to start a session .356 How to set and get session variables 358 How to end a session 360 How to manage a session 362 The S h op p in g Cart a p p lic a tio n .364 The user interface 364 The controller 366 The m odel 368 The Add Item v ie w .370 The Cart v ie w 372 Chapter 13 How to create and use functions B a sic sk ills for working with f u n c t io n s 380 How to create and call a function 380 How to pass arguments by value and by reference 382 How variable scope works 384 How to provide default values for parameters 386 How to use variable-length parameter lists 388 How to create and u s e a library of fu n c tio n s .390 A library of functions .390 How to set the include path 392 How function scope w orks 392 How to create and use namespaces 394 Expanded contents A dvanced sk ills for working with fu n c tio n s 396 How to work with variable functions and callbacks 396 How to work with anonymous functions 398 How to work with closures 400 The S h op p in g Cart a p p lic a tio n .402 The user interface 402 The m odel 404 The controller 406 The v ie w 406 Chapter 14 How to create and use objects How to create and u s e c l a s s e s 414 The code for the Category class 414 The code for the Product class 416 How to code properties 420 How to code constructors and destructors 422 How to code methods 424 How to create and use objects 426 How to c o d e c la s s c o n sta n ts, properties, and m e t h o d s 428 How to code class constants 428 How to code static properties and methods 430 The object-oriented Product Manager a p p lica tio n 432 The user interface 432 The m odel 434 The controller 438 The v ie w 440 Additional sk ills for working with o b je c t s 442 How to loop through an object’s properties 442 How to clone and compare objects 444 How to inspect an object 446 How to work with in h erita n ce 448 How to inherit a cla ss .448 How to use the protected access modifier 450 How to create abstract classes and methods 452 How to create final classes and methods 454 How to work with interfaces 456 Chapter 15 How to use regular expressions, handle exceptions, and validate data How to u s e regular e x p r e s s io n s .462 How to create and use regular expressions 462 How to match characters 464 How to use the character class 466 How to create complex patterns 468 How to use look-ahead assertions 470 How to use a multiline regular expression 472 How to use a global regular expression 472 How to replace a regular expression with a string 474 How to split a string on a regular expression 474 Regular expressions for data validation 476 How to handle e x c e p t io n s 478 How to create and throw exceptions .478 How to use the try-catch statement 480 ... with PHP and MySQL Chapter Introduction to web development with PHP Chapter How to code a PHP application Chapter Introduction to relational databases and MySQL Chapter How to use PHP with a MySQL... 18 The PHP f ile 20 How to ed it and te s t a PHP a p p lica tio n 22 How to edit a PHP page with a text editor .22 How to start and stop Apache and MySQL on... introduction to M ySQL 106 What MySQL provides 106 Two ways to work with MySQL 108 How to u s e phpM yAdm in 110 How to start phpMyAdmin