You are on page 1of 14

MySQL and PHP - Developing

Dynamic Web Applications


Student Guide
SQL-4405 Rev 2.0

D62048GC10
Edition 1.0
D63883

Copyright 2010, Oracle and/or its affiliates. All rights reserved.


Disclaimer
This document contains proprietary information, is provided under a license agreement containing restrictions on use and
disclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely for
your own use in an Oracle training course. The document may not be modified or altered in any way. Except as expressly
permitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express
authorization of Oracle.
The information contained in this document is subject to change without notice. If you find any problems in the document,
please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This
document is not warranted to be error-free.
Sun Microsystems, Inc. Disclaimer
This training manual may include references to materials, offerings, or products that were previously offered by Sun
Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.Oracle and its
affiliates cannot be held responsible for any such references should they appear in the text provided.
Restricted Rights Notice
If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S.
Government, the following notice is applicable:
U.S. GOVERNMENT RIGHTS
The U.S. Governments rights to use, modify, reproduce, release, perform, display, or disclose these training materials are
restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.

Trademark Notice
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro
Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used
under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark
licensed through X/Open Company, Ltd.

MySQL and PHP - Developing Dynamic Web Applications

Preface

MySQL and PHP - Developing Dynamic Web Applications Student Guide


This is the Student guide for the MySQL and PHP - Developing Dynamic Web Applications training course. This
guide was developed using the MySQL 5.0 and/or MySQL 5.1 Community Edition - Generally Available (GA)
Releases.

iii

MySQL and PHP - Developing Dynamic Web Applications

Preface

Conventions used in this training guide


The following typographical conventions are used throughout this training guide:

Computer input and output is printed in this format: Computer input or output. This is also used
for the names of executable programs and file locations.

Keywords from the SQL language appear in this format: SQL KEYWORD. SQL keywords are not case
sensitive and may be written in any letter case, but the training guide uses uppercase.

Placeholders for user input inside appearing inside computer input appear in this format: <user input>

For emphasis, the folowing style is used: Emphasis

For extra emphasis, the following style is used: Extra Emphasis

When commands are shown that are meant to be executed from within a particular program, the prompt shown
preceding the command indicates which command to use. For example, shell> indicates a command that you
execute from your shell, and mysql> indicates a statement that you execute from the mysql client program:
shell> mysql -u root -h 127.0.0.1
mysql> SELECT * FROM world.City;
The shell is your command interpreter. On Linux, this is typically a program such as sh, csh, or bash. On
Windows, the equivalent program is command.com or cmd.exe, typically run in a console window. When you enter
a command or statement shown in an example, do not type the prompt shown in the example.
Database, table, and column names must often be substituted into statements. To indicate that such substitution is
necessary, this manual uses db_name, tbl_name, and col_name. For example, you might see a statement like
this:
mysql> SELECT col_name FROM db_name.tbl_name;
This means that if you were to enter a similar statement, you would supply your own database, table, and column
names for the placeholdes db_name, tbl_name, and col_name., perhaps like this:
mysql> SELECT author_name FROM biblio_db.author_list;
In syntax descriptions, square brackets ([ and ]) indicate optional words or clauses. For example, in the following
statement, IF EXISTS is optional:
DROP TABLE [IF EXISTS] tbl_name;
When a syntax element consists of a number of alternatives, the alternatives are separated by vertical bars (pipe, |).
When one member from a set of choices may be chosen, the alternatives are listed within square brackets ([ and ]):
TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
When one member from a set of choices must be chosen, the alternatives are listed within braces ({ and }):
{DESCRIBE | DESC} tbl_name [col_name | wild]

iv

MySQL and PHP - Developing Dynamic Web Applications

Preface

An ellipsis (...) indicates the omission of a section of a statement, typically to provide a shorter version of more
complex syntax. For example, INSERT ... SELECT is shorthand for the form of INSERT statement that is
followed by a SELECT statement.
An ellipsis can also indicate that the preceding syntax element of a statement may be repeated. In the following
example, multiple reset_option values may be given, with each of those after the first preceded by commas:
RESET reset_option[, reset_option] ...
Commands for setting shell variables are shown using Bourne shell syntax. For example, the sequence to set the CC
environment variable and run the configure command looks like this in Bourne shell syntax:
shell> CC=gcc ./configure
If you are using csh or tcsh, you must issue commands somewhat differently:
shell> setenv CC gcc
and
shell> ./configure

MySQL and PHP - Developing Dynamic Web Applications

Preface

Supporting images used in this Training Guide


The following is a summary of the standard images used in this manual to support the instruction:
IMAGE

123

vi

NAME

DESCRIPTION

Preparation

This image is used to describe the steps required to be completed


prior to performing a hands-on exercise.

Written exam

This image is used to identify that the student is going to be tested


upon the material previously presented in the instructional material

Inline Lab

Throughout the course the instructor will conduct labs in line with
the instruction, which are designed to help you to understand the
nuts and bolts (inner-workings) of the topic.

Further Practice
Lab

This image is used to convey to the student that there is a final


exercise to complete prior to the completion of the chapter.

Student notes

This image identifies an area on a page designated for students to


write notes associated with the class.

Slide number box

Indicates the number of an existing slide that corresponds to the text.

MySQL and PHP - Developing Dynamic Web Applications

Preface

Acknowledgments
Sun Microsystems would like to thank the many individuals that played a part in bringing this training material to
the numerous students who will benefit from the knowledge and effort that each of these contributors put into the
training. Even though there were a large number of contributions from many Sun Microsystems' employees, the
following list of contributors played a vital role in developing this material and ensuring that its contents were
accurate, timely and most of all presented in a way that would benefit those that are utilizing it for the benefit of
improving their skills with MySQL.
Jeffrey Gorton - Course Developer and Editor
Max Mether - Course Development Manager
Kai Voigt - Lead Instructor/Subject Matter Expert
Tom Hanlon Subject Matter Expert

vii

MySQL and PHP - Developing Dynamic Web Applications

Preface

Course Objectives
Through a hands-on approach, this instructor-led course will help you add to your knowledge the skills to combine
PHP and MySQL with time-proven database management techniques to create best-of-breed web applications that
are efficient, solid and secure. At the completion of this course you should be able to:

Describe the LAMP architecture

Use the basic components of PHP to build a foundation for more complex web applications

Understand the basic components of MySQL to build a foundation for the development of fully dynamic
and database-driven sites

Use SQL query commands to retrieve data from the MySQL Database

Change table data using the SQL Data Manipulation Language (DML) commands

Retrieve data from multiple MySQL tables using Joins

Create web based forms that interact with the end user and the data within MySQL

Use session handling to authenticate and monitor user identities

Describe the purpose of template systems

xi

MySQL and PHP - Developing Dynamic Web Applications

Preface

Table of Contents
1 Introduction............................................................................................................................. 1-0
1.1 Learning Objectives......................................................................................................... 1-1
1.2 MySQL Overview............................................................................................................1-2
1.2.1 Sun Acquisition........................................................................................................ 1-2
1.2.2 MySQL Partners.......................................................................................................1-3
1.3 MySQL Products............................................................................................................. 1-4
1.3.1 MySQL Database Server Products...........................................................................1-4
1.3.2 MySQL GUI Tools................................................................................................... 1-4
1.3.3 MySQL Connectors..................................................................................................1-5
1.3.4 Solutions for Embedding MySQL........................................................................... 1-6
1.4 MySQL Services.............................................................................................................. 1-7
1.4.1 MySQL Training...................................................................................................... 1-7
1.4.2 MySQL Certification................................................................................................1-7
1.4.3 MySQL Consulting.................................................................................................. 1-7
1.4.4 MySQL Support....................................................................................................... 1-7
1.5 The MySQL Enterprise Subscription...............................................................................1-9
1.5.1 MySQL Enterprise Server........................................................................................ 1-9
1.5.2 24x7 Production Support..........................................................................................1-9
1.5.3 MySQL Enterprise Monitor................................................................................... 1-10
1.5.4 Obtaining a MySQL Enterprise Subscription........................................................ 1-11
1.6 Supported Operating Systems........................................................................................1-13
1.7 MySQL Certification Program.......................................................................................1-14
1.8 Training Curriculum Paths.............................................................................................1-15
1.9 MySQL Website.............................................................................................................1-19
1.9.1 MySQL Community Web Page..............................................................................1-20
1.9.2 MySQL Online Documentation............................................................................. 1-22
1.10 Chapter Summary........................................................................................................ 1-25
2 LAMP...................................................................................................................................... 2-1
2.1 Learning Objectives......................................................................................................... 2-2
2.2 LAMP Architecture..........................................................................................................2-3
2.3 Apache Server.................................................................................................................. 2-4
2.3.1 Understanding the World Wide Web........................................................................ 2-4
2.3.2 Client Side vs. Server Side scripting........................................................................2-6
2.4 The MySQL Database......................................................................................................2-7
2.4.1 What is a RDBMS?.................................................................................................. 2-7
2.4.2 Entities and Relationships........................................................................................ 2-8
2.4.3 RDBMS Database Structure.................................................................................... 2-9
2.5 PHP................................................................................................................................ 2-10
2.5.1 How is PHP used?.................................................................................................. 2-10
2.5.2 How does PHP work?.............................................................................................2-11
2.5.3 PHP Design Consideration.....................................................................................2-12
2.5.4 Comparing PHP to other tools............................................................................... 2-13
2.6 Installing XAMPP..........................................................................................................2-15
2.7 Chapter Summary.......................................................................................................... 2-20
3 PHP Foundations..................................................................................................................... 3-1
3.1 Learning Objectives......................................................................................................... 3-2
3.2 Delimiting PHP Code...................................................................................................... 3-3
xii

MySQL and PHP - Developing Dynamic Web Applications

Preface

3.3 Comments ....................................................................................................................... 3-5


3.4 Construct Feedback..........................................................................................................3-7
3.5 Obtaining PHP Information........................................................................................... 3-11
3.6 PHP Data Types............................................................................................................. 3-15
3.6.1 Scalar Data Types...................................................................................................3-15
3.6.2 Compound Data Types........................................................................................... 3-17
3.6.3 Special Data Types................................................................................................. 3-18
3.7 Flow Control Statements............................................................................................... 3-19
3.7.1 Comparison Operators........................................................................................... 3-19
3.7.2 if ... else ... elseif.................................................................................................... 3-20
3.7.3 Switch.....................................................................................................................3-22
3.7.4 While...................................................................................................................... 3-23
3.7.5 Do ... While............................................................................................................ 3-24
3.7.6 For.......................................................................................................................... 3-25
3.7.7 Foreach...................................................................................................................3-26
3.8 PHP Functions............................................................................................................... 3-28
3.8.1 User Defined Functions..........................................................................................3-29
3.9 Chapter Summary.......................................................................................................... 3-32
4 MySQL Foundations............................................................................................................... 4-1
4.1 Learning Objectives......................................................................................................... 4-2
4.2 MySQL General Architecture.......................................................................................... 4-3
4.2.1 MySQL Server ........................................................................................................ 4-3
4.2.2 Client Programs........................................................................................................4-4
4.3 Storage Engines............................................................................................................... 4-5
4.4 The mysql Client..............................................................................................................4-6
4.4.1 Using mysql Interactively ....................................................................................... 4-6
4.4.2 Statement Terminators .............................................................................................4-7
4.4.3 Using script files with mysql....................................................................................4-8
4.5 Table Properties..............................................................................................................4-11
4.6 SELECT Statements...................................................................................................... 4-14
4.6.1 SELECT with DISTINCT......................................................................................4-16
4.6.2 SELECT with WHERE.......................................................................................... 4-17
4.6.3 SELECT with ORDER BY.................................................................................... 4-20
4.6.4 SELECT with LIMIT............................................................................................. 4-21
4.7 Aggregating Query Results............................................................................................ 4-24
4.8 Chapter Summary.......................................................................................................... 4-30
5 PHP Connecting to MySQL.................................................................................................... 5-1
5.1 Learning Objectives......................................................................................................... 5-2
5.2 Connecting to MySQL.....................................................................................................5-3
5.2.1 Independent Connection Information...................................................................... 5-8
5.2.2 Securing Connection Information.......................................................................... 5-11
5.3 Retrieving Data.............................................................................................................. 5-12
5.4 Chapter Summary.......................................................................................................... 5-20
6 Linking Between Tables.......................................................................................................... 6-1
6.1 Learning Objectives......................................................................................................... 6-2
6.2 HTML Links.................................................................................................................... 6-3
6.2.1 Anchor Tag............................................................................................................... 6-3
6.3 Passing Data between Web Pages....................................................................................6-6

xiii

MySQL and PHP - Developing Dynamic Web Applications

Preface

6.4 Link one dynamic page to another.................................................................................6-11


6.5 Chapter Summary.......................................................................................................... 6-18
7 SQL DML Commands.............................................................................................................7-1
7.1 Learning Objectives......................................................................................................... 7-2
7.2 The INSERT Statement....................................................................................................7-3
7.2.1 INSERT with SELECT............................................................................................ 7-4
7.2.2 INSERT with LAST_INSERT_ID........................................................................... 7-4
7.2.3 INSERT with NULL Values.....................................................................................7-5
7.3 The UPDATE Statement.................................................................................................. 7-6
7.3.1 UPDATE with SET.................................................................................................. 7-7
7.3.2 UPDATE with ORDER BY..................................................................................... 7-7
7.3.3 UPDATE with LIMIT.............................................................................................. 7-7
7.4 The REPLACE Statement................................................................................................7-9
7.4.1 INSERT with ON DUPLICATE KEY UPDATE Instead of REPLACE...............7-10
7.5 The DELETE Statement................................................................................................ 7-11
7.6 Chapter Summary.......................................................................................................... 7-18
8 MySQL Data Driven Web Based Forms................................................................................. 8-1
8.1 Learning Objectives......................................................................................................... 8-2
8.2 HTML Web Forms........................................................................................................... 8-3
8.2.1 Designing Web Forms.............................................................................................. 8-3
8.2.2 Reading Web Form Submissions........................................................................... 8-13
8.3 Combining MySQL and HTML Forms......................................................................... 8-18
8.4 Updating MySQL based on Form Submissions.............................................................8-27
8.4.1 Updating MySQL with Form Data.........................................................................8-27
8.4.2 Deleting MySQL Data with PHP........................................................................... 8-32
8.4.3 Inserting MySQL Data with PHP...........................................................................8-36
8.5 Chapter Summary.......................................................................................................... 8-45
9 SQL JOINS.............................................................................................................................. 9-1
9.1 Learning Objectives......................................................................................................... 9-2
9.2 What is a Join?................................................................................................................. 9-3
9.3 Inner Joins........................................................................................................................9-5
9.3.1 Comma Separated Inner Joins..................................................................................9-5
9.4 Using Qualifiers and Aliases............................................................................................9-7
9.4.1 Qualifying Column Names...................................................................................... 9-7
9.5 Chapter Summary.......................................................................................................... 9-11
10 Session Handling................................................................................................................. 10-1
10.1 Learning Objectives..................................................................................................... 10-2
10.2 What is Session Handling?.......................................................................................... 10-3
10.3 Session Handling Tasks............................................................................................... 10-5
10.4 Session Handling and Security.................................................................................... 10-7
10.5 Database Authentication............................................................................................ 10-13
10.6 Chapter Summary...................................................................................................... 10-19
11 Template Systems................................................................................................................ 11-1
11.1 Learning Objectives..................................................................................................... 11-2
11.2 Template Systems.........................................................................................................11-3
11.3 Smarty Template Engine.............................................................................................. 11-4
11.3.1 Object Oriented Programming............................................................................. 11-5
11.4 Chapter Summary.......................................................................................................11-15

xiv

MySQL and PHP - Developing Dynamic Web Applications

Preface

12 Conclusion........................................................................................................................... 12-1
12.1 Course Overview......................................................................................................... 12-2
12.2 Training and Certification Website............................................................................. 12-3
12.3 Course Evaluation........................................................................................................12-5
12.4 Thank you!................................................................................................................... 12-6
12.5 Q&A Session................................................................................................................12-7
Appendix A Solutions................................................................................................Appendix A-0
Appendix B Session Handling Configuration Options............................................. Appendix B-0
Appendix C Common Programming Tools...............................................................Appendix C-0

xv

You might also like