TRAINING & REFERENCE m M urach m urach'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 6 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 w ith 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 ike M urach & Associates, Inc. 1-800-221-5528 · (559) 440-9071 · Fax: (559) 440-0963 [email protected] · 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 4 Web Programming with C# 2010 Murach’s ASP.NET 4 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 4 Database Programming with C# 2010 Murach’s ADO.NET 4 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 6 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 1 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 987654321 ISBN-13: 978-1-890774-56-1 Contents Introduction Section 1 Get started fast with PHP and MySQL Chapter 1 Introduction to web development with PHP 3 Chapter 2 How to code a PHP application 45 Chapter 3 Introduction to relational databases and MySQL 93 Chapter 4 How to use PHP with a MySQL database 123 Chapter 5 How to use the MVC pattern to organize your code 159 Chapter 6 How to test and debug a PHP application 189 Section 2 Master PHP programming Chapter 7 How to work with form data 207 Chapter 8 How to code control statements 231 Chapter 9 How to work with strings and numbers 259 Chapter 10 How to work with dates 291 Chapter 11 How to create and use arrays 311 Chapter 12 How to work with cookies and sessions 347 Chapter 13 How to create and use functions 379 Chapter 14 How to create and use objects 413 Chapter 15 How to use regular expressions, handle exceptions, and validate data 461 Section 3 Master MySQL programming Chapter 16 How to design a database 503 Chapter 17 How to use SQL to create a MySQL database 537 Chapter 18 How to use SQL to work with a MySQL database 573 Chapter 19 Professional PHP for working with MySQL 611 Chapter 20 A database-driven web site 645 Section 4 Master the advanced skills for building web sites Chapter 21 How to create secure web sites 675 Chapter 22 How to send email and access other web sites 707 Chapter 23 How to work with files, uploads, and images 743 Chapter 24 An eCommerce web site 779 Reference Aids Appendix A How to set up your PC for this book 801 Appendix B How to set up your Mac for this book 817 Index 832 Expanded contents Expanded contents Section 1 Get started fast with PHP and MySQL Chapter 1 Introduction to web development with PHP The architecture of a web application......................................................................4 How a client-server architecture works...............................................................................4 How static web pages are processed.....................................................................................6 How dynamic web pages are processed..............................................................................8 A survey of web application software............................................................................. 10 Highlights in the history of PHP....................................................................................... 12 Highlights in the history of MySQL................................................................................ 12 The Product Discount application...........................................................................14 The user interface............................................................................................................... 14 The HTML file................................................................................................................... 16 The CSS file......................................................................................................................... 18 The PHP file.........................................................................................................................20 How to edit and test a PHP application.................................................................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 page...................................................................32 How to use NetBeans to develop a PHP application.......................................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 2 How to code a PHP application Basic PHP skills..............................................................................................................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 get data from a request.................................................................................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 data...................................................................................................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 Discount application...........................................................................66 The user interface.................................................................................................................66 The form in the HTML file.................................................................................................66 The PHP file.........................................................................................................................68 Expanded contents How to code control statements...............................................................................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 application......................................................................................78 The user interface.................................................................................................................78 The code for the index.php file............................................................................................80 The code for the display_results.php file............................................................................82 How to use the PHP documentation........................................................................86 How to access the PHP manual...........................................................................................86 How to find the documentation you need...........................................................................86 Chapter 3 Introduction to relational databases with MySQL An introduction to relational databases.................................................................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 statements for data manipulation.....................................................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 MySQL......................................................................................106 What MySQL provides..................................................................................................... 106 Two ways to work with MySQL...................................................................................... 108 How to use phpMyAdmin........................................................................................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 4 How to use PHP with a MySQL database PHP for working with MySQL................................................................................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 get data from a result set.........................................................................132 How to work with arrays.................................................................................................. 132 How to get the data from the first row of a result set.................................................. 134 How to get the data from all the rows of a result set................................................... 136 The Product Viewer application...........................................................................138 The user interface............................................................................................................. 138 The code...............................................................................................................................140 The Product Manager application........................................................................146 The user interface............................................................................................................. 146 The code............................................................................................................................. 148 Expanded contents Chapter 5 How to use the MVC pattern to organize your code How to use the MVC pattern..................................................................................160 An introduction to the MVC pattern............................................................................... 160 How to code functions..................................................................................................... 162 How to redirect requests.................................................................................................. 164 The Product Manager application.......................................................................166 The user interface............................................................................................................. 166 The model.......................................................................................................................... 168 The controller................................................................................................................... 170 The view............................................................................................................................ 172 The Product Catalog application.........................................................................178 The user interface............................................................................................................. 178 The model.......................................................................................................................... 180 The controller................................................................................................................... 180 The view............................................................................................................................ 182 Chapter 6 How to test and debug a PHP application An introduction to testing and debugging.......................................................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 debug with xDebug and NetBeans......................................................196 How to set and remove breakpoints................................................................................ 196 How to step through code................................................................................................ 198 How to inspect variables.................................................................................................. 198 How to inspect the stack trace..........................................................................................200 Section 2 Master PHP programming Chapter 7 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 box..............................................................................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 8 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 VÏÏ 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 loops................................................................................................248 How to code do-while loops...........................................................................................250 How to code for loops......................................................................................................252 How to use the break and continue statements...............................................................254 Chapter 9 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 strings and 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 DateTime classes 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 viii Expanded contents How to create and use an associative array......................................................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 use functions to work with arrays...........................................................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 elements..................................................................328 How to search arrays..........................................................................................................328 How to sort arrays...............................................................................................................330 How to modify arrays.........................................................................................................332 How to work with arrays of arrays.........................................................................334 Understanding an array of arrays......................................................................................334 How to create and use an array of arrays..........................................................................336 The Task List Manager application........................................................................338 The user interface...............................................................................................................338 The code for the controller................................................................................................338 The code for the view.........................................................................................................340 Chapter 12 How to work with cookies and sessions How to work with cookies..........................................................................................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 sessions.......................................................................................354 Why session tracking is difficult with HTTP..................................................................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 Shopping Cart application...............................................................................364 The user interface...............................................................................................................364 The controller......................................................................................................................366 The model............................................................................................................................368 The Add Item view.............................................................................................................370 The Cart view.....................................................................................................................372 Chapter 13 How to create and use functions Basic skills for working with functions...............................................................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 use a library of functions.....................................................390 A library of functions.........................................................................................................390 How to set the include path...............................................................................................392 How function scope works................................................................................................392 How to create and use namespaces...................................................................................394 Expanded contents Advanced skills for working with functions.......................................................396 How to work with variable functions and callbacks.......................................................396 How to work with anonymous functions.........................................................................398 How to work with closures................................................................................................400 The Shopping Cart application...............................................................................402 The user interface...............................................................................................................402 The model............................................................................................................................404 The controller.....................................................................................................................406 The view..............................................................................................................................406 Chapter 14 How to create and use objects How to create and use classes................................................................................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 code class constants, properties, and methods..............................428 How to code class constants..............................................................................................428 How to code static properties and methods.....................................................................430 The object-oriented Product Manager application..........................................432 The user interface...............................................................................................................432 The model............................................................................................................................434 The controller.....................................................................................................................438 The view..............................................................................................................................440 Additional skills for working with objects...........................................................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 inheritance..................................................................................448 How to inherit a class.........................................................................................................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 use regular expressions.............................................................................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 exceptions........................................................................................478 How to create and throw exceptions.................................................................................478 How to use the try-catch statement...................................................................................480
Description: