M u r a c h T R A IN IN G & REFERENCE 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 My
Trang 1M u r a c h
T R A IN IN G & REFERENCE
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 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
Trang 2TRAINING & REFERENCE
and MySQL
Joel Murach Ray Harris
Mik e Mu ra ch & Asso cia tes, In c 1-800-221-5528 · (559) 440-9071 · Fax: (559) 440-0963 murachbooks@murach.com · www.murach.com
Trang 3Authors: Joel Murach
Ray Harris Mike Murach Ben Murach Zylka Design
Editor:
Cover 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 2008fo r 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 9 8 7 6 5 4 3 2 1
ISBN-13: 978-1-890774-56-1
Trang 4Introduction
Section 1 Get started fast w ith PHP and MySQL
Section 2 Master PHP programming
Section 3 Master MySQL programming
Section 4 Master the advanced skills for building web sites
Reference Aids
Trang 5Expanded contents
Expanded contents
Section 1 Get started fast with PHP and MySQL
Chapter 1 Introduction to web development with PHP
T he architecture o f a w eb a p p lic a tio n 4
How a client-server architecture w orks 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
T he Product D iscou n t a p p lic a tio n 14
The user interface 14
The HTML f i l e 16
The CSS file 18
The PHP f i l e 20
How to ed it and t e s t a PHP a p p lic a 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 a g e 32
How to u s e N etB ea n s to d e v 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 2 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 e t 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
T he Product D iscou n t a p p lic a tio n 66
The user interface 66
The form in the HTML file 66
The PHP f i l e 68
Trang 6Expanded contents
How to c o d e control s t a t e 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 lic a 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 c u m e n ta tio n 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 d a t a 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 te m e n 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 ySQ L 106
What MySQL provides 106
Two ways to work with MySQL 108
How to u s e phpM yA dm 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 4 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 e t data from a resu lt 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 View er a p p lic a tio n 138
The user interface 138
The c o d e 140
The Product M anager a p p lic a tio n 146
The user interface 146
The c o d e 148
Trang 7Expanded contents
Chapter 5 How to use the MVC pattern to organize your code
How to u s e th e MVC p a tter n 160
An introduction to the MVC pattern 160
How to code functions 162
How to redirect requests 164
T he Product M anager a p p lic a tio n 166
The user interface 166
The m odel 168
The controller 170
The v ie w 172
T he 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 6 How to test and debug a PHP application An introduction to te stin 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 eb u g 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 i o n 2 M a s t e r P H P p r o g r a m m in g 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 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 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
Trang 8Expanded 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 lo o p s 248
How to code do-while loop s 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 valu es 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 d ates 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
Trang 9v iii Expanded contents
How to cr ea te and u s e an a s s o c ia tiv e a rr a 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 c tio n s to work with a rra y s 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 w ork 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
T he Task List M anager 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 i e s 348
An introduction to cook ies 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
T he S h o p 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 cr ea te and u s e a library o f fu n c t io 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
Trang 10Expanded contents
A dvan ced 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 o p 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 cr ea te 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, prop erties, and m e t h o d s 428
How to code class constants 428
How to code static properties and methods 430
The ob ject-orien ted Product M anager a p p lica tio n 432
The user interface 432
The m odel 434
The controller 438
The v ie w 440
A dditional sk ills for working with o b j e 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 e r ita n c e 448
How to inherit a c la s s 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 i o 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 h and le 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