Вы находитесь на странице: 1из 81

Introduction to PHP & MySQL

A 4 days Course Presented by

Shaizar Md Daud @ Jat

Course Outline

Introduction to HTML Introduction to PHP PHP - The Language Using Variables Giving Your Code Life Flow Control Using Functions Creating Custom Functions Making Things Constant Variable Scope Now You See Me, Now You Dont! Introduction to Database & MySQL Using MySQL from PHP Putting things Together Lab Project
9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaizar.com)

Day 1

Introduction to HTML Introduction to PHP PHP - The Language

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

What is HTML?

Hyper-Text Markup Language

Language for creating web pages Defines format & layout of a document Documents are Portable Link multiple documents Local and remote documents A simple text file Tag-based language
Trainer: Shaizar Md Daud (jat@shaizar.com)

9 - 12 April 2007

HTML Markup Tags

Single Tags

No Content or Child Tags

Syntax: <tagname/> or <tagname> Example:

<br/>, <br>, <hr>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Markup Tags, contd

Start and End Tags

Contains Content or Child Tags

Syntax: <tagname>Some Content</tagname> Example:

<p>The quick brown fox jumps over the lazy dog</p>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Comments

Comments are not rendered (displayed)

Syntax: <!-- comment text --> Example: <!--

This is a HTML comment


-->

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Document Structure


Document Type Declaration HTML Container


HTML Header HTML Body

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

DOCTYPE
HTML Document Structure

Common DOCTYPEs:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Container Tags


HTML Document Structure
Start Tag <html> <head> <body> End Tag </html> </head> </body> Description HTML Container HTML Header Section HTML Body Section

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Layout Tags


Start Tag <p> <br/> <div> End Tag </p> </div> Description Paragraph Text Insert Line Break Division Block

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Text Formatting Tags


Start Tag <b> <strong> <i> End Tag </b> </strong> </i> Description Bold Text Bold Text Italic Text

<u>
<hr/> <hn> <span>

</u>
</hn> </span>

Underline Text
Horizontal Line Text Headers [ n = 1 6 ] Apply Format Elements to Text
Trainer: Shaizar Md Daud (jat@shaizar.com)

9 - 12 April 2007

HTML List Tags


Start Tag <ol> <ul> <li> End Tag </ol> </ul> </li> Description Ordered List Unordered List List Item

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Ordered List Tag


Syntax: <ol type=[ 1 | I | i | A | a ]> [ list items ]</ol>

Example: <ol>
<li>Item 1</li> <li>Item 2</li> </ol>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Unordered List Tag


Syntax: <UL type=[ DISC | CIRCLE | SQUARE ]> [ list items ]</UL>

Example: <UL>
<li>Item 1</li> <li>Item 2</li> </UL>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Table
Start Tag <table> <tr> <td> End Tag </table> </tr> </td> Description Table Container Table Row Table Data

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Table Example


<table border=0 cellpadding=2 cellspacing=2 width=600> <tr> <td width=200>Row 1, Column 1</td> <td width=400>Row 1, Column 2</td> </tr> <tr> <td align=left valign=center>Row 2, Column 1</td> <td>Row 2, Column 2</td> </tr> </table>
Trainer: Shaizar Md Daud (jat@shaizar.com)

9 - 12 April 2007

HTML Tags HTML Links


Start Tag <a href=[url]> <a href=#myId> End Tag </a> </a> Description Anchor link to a URL. Anchor link to an Target ID.

<a id=myId>

</a>

Target ID to link to.

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

HTML Forms
Start Tag
<form> <input /> <select> <option> <textarea>

End Tag
</form> </select> </option> </textarea>

Description
Form Container Input Fields Select Box Container Select Box Options Text Box

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Form Tag
Syntax: <FORM action=<url> method=[ GET | POST ]> Example: <FORM action=register.php method=post> . . . . . . </FORM>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Input Tag

Attributes

type

TEXT | CHECKBOX | RADIO | PASSWORD | HIDDEN | SUBMIT | RESET | BUTTON

name value size maxlength checked


9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaizar.com)

Input Tag, contd


Example: <INPUT type=text name=fullname size=50/> <INPUT type=password name=pass size=50/>

<INPUT type=radio name=sex value=m/> Male


<INPUT type=radio name=sex size=f/> Female <INPUT type=checkbox name=class size=php_b/> <INPUT type=checkbox name=class size=php_i/> <INPUT type=checkbox name=active size=1/> Yes <INPUT type=submit name=cmdSave value=Save/>
9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaizar.com)

Select Tag

Attributes

name size multiple

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Option Tag

Used within SELECT tags. Defines SELECT Items. Attributes


value selected

CDATA = Select item label

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

SELECT OPTION
Example: <SELECT name=addr_state size=1> <OPTION value=sel selected>Selangor</OPTION>

<OPTION value=joh>Johor</OPTION>
<OPTION value=ked>Kedah</OPTION> </SELECT>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

SELECT OPTION (multiple)


Example: <SELECT name=avail_state size=10 multiple> <OPTION value=sel selected>Selangor</OPTION>

<OPTION value=joh>Johor</OPTION>
<OPTION value=ked selected>Kedah</OPTION> </SELECT>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

TextArea Tag

Attributes

rows cols wrap: [ soft | hard | off ] Text to be displayed in the form field.

CDATA

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

TextArea Tag, contd


Example: <TEXTAREA name=notes rows=10 cols=40> Text displayed in TextArea form field.

It supports multiple text lines.


</TEXTAREA>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

What is PHP

Scripting Language Server-side Scripting Embedded Scripts Based on C, C++, and Perl Create web pages dynamically Multi-platform (*nix, Windows) Supports Apache and IIS web server CGI or Server Module
9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaizar.com)

Server-side vs Client-side

Server-side

Client-side

Pro

Pro

More secure Client independent Light-weight Client Easier deployment

Better response Richer user interface No or lighter server requirements Less secure Client dependent Challenging Deployment
Trainer: Shaizar Md Daud (jat@shaizar.com)

Con

Con

Powerful hardware High server resource High Network resource

9 - 12 April 2007

PHP Basic Construct


<html> <body> HTML Section <h1>HTML Title</h1>

<?php
echo Hello from PHP; PHP Section ?> </body> HTML Section </html>

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Language Syntax

Start PHP Section

<?php or <? ?> A_php_statement;

End PHP Section

PHP Statement

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

PHP Comments

Not executed Source-code documentation Disable parts of codes C-style Comments


// Single line comment /* Multi-line comment */

Perl-style Comments (not recommended)

# Perl-style single line comment

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Day 2

Using Variables Giving Your Code Life Flow Control

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Data Types
Data Type String Integer Float Boolean Array Object Resource Database, file
9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaizar.com)

Description text, text -2, -1, 0, 1, 2 0.4, 1.234 True, false, 0, <none zero>

Variables

Syntax

$my_variable_1 $_my_variable

Case sensitive Container to hold a value or values Values can be changed Variable name starts with an alphabet or underscore Variable name consist of alphabets, underscores, and numbers
Trainer: Shaizar Md Daud (jat@shaizar.com)

9 - 12 April 2007

Giving Variables a Value

Examples

$my_string = My String; $my_numbers = 123; $fn_return_value = my_function();

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Arrays

Stores multiple values in a single variable Stored values can be of different types Index-based or Key-based

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Index-based Array

Numeric indexes Examples


$index_array[0] = Item 1; $index_array[1] = Item 2; $index_array[2] = Item 3;

Index starts from 0

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Associate Array

Keys instead of numeric indexes Examples


$kb_array[one] = Item 1; $kb_array[two] = Item 2; $kb_array[three] = Item 3;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Constants

Value is fixed once created Example Creating constants


define(MYCONST, 23); define(MYCONSTSTR, My String);


print The value of MYCONST: . MYCONST;

Example Using constants

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Operators
Operator = Description Assignment Operator + Description Addition

==
=== !

Equality
Exact Equality Not

++
--

Increment
Subtraction Decrement

&&
|| >, >= <, <=

Logical AND
Logical OR Greater (or Equal) Less (or Equal)

*
/ % .

Multiply
Division Modulo Concatenate (string)
Trainer: Shaizar Md Daud (jat@shaizar.com)

9 - 12 April 2007

Form Variables

$_GET[<field_name>] $_POST[<field_name>] $_REQUEST[<field_name>]

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Flow Control

Conditional Statements Loops

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

IF Statement
Syntax: if ( <condition> ) { [ block to be executed if <condition> is true ]

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

IF ELSE
Syntax: if ( <condition> ) { [ block to be executed if <condition> is true ]

}
else { [ block to be executed if <condition> is false ] }

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

IF ELSE IF
Syntax: if ( <condition1> ) { [ block to be executed if <condition1> is true ]

} else if ( <condition2> ) { [ block to be executed if <condition2> is true ] }

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Nested IF
Syntax: if ( <condition1> ) { [ block to be executed if <condition1> is true ]

if ( <condition2> ) { [ block to be executed if <condition2> is true ] } [ continue ]


}

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Example of IF Statements
if( $_POST[location] == sel ) { print Selangor; } if( $_POST[age] <=18 ) { print You must be above 18; } else { print You may proceed; }
9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaizar.com)

Loops

WHILE loop FOR loop FOREACH loop

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

WHILE Loop
Syntax: while ( <condition> ) { [ block to be executed

while <condition> is true ]


}

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

WHILE Loop Example


Example: $i = 0; while( $i print . . $i++; }

< sizeof( $profile ) ) { Item $i: $profile[ $i ] <br />;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

FOR Loop
Syntax: for ( <init>; <condition>; <iterator> ) { [ block to be executed

while <condition> is true ]


}

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

FOR Loop Example


Example: for ( $i = 0; $i < sizeof( $profile ); $i++ ) { print Item $i: . $profile[ $i ] . <br />; }

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

FOREACH Loop
Syntax 1: foreach ( <array> as <var_value> ) { [ block to be executed for each item in array ]

}
Syntax 2: foreach ( <array> as <var_key> => <var_value> ) { [ block to be executed for each item in array ] }

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

FOREACH Loop Example


Example 1: foreach ( print . . } Example 2: foreach ( print . . } $profile as $item ) { Item: $item <br />;

$profile as $key => $item ) { Item . $key . : $item <br />;


Trainer: Shaizar Md Daud (jat@shaizar.com)

9 - 12 April 2007

Day 3

Using Functions Creating Custom Functions Making Things Constant Variable Scope Now You See Me, Now You Dont!

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Using Functions
Syntax: function_name([<param>]) Example:

echo date();
echo date( d-m-Y ); $a = sprintf( hello );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

User-defined functions
Syntax: function function_name() { }

function function_name($param1, $param2=2) {


return $rv; }

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Variable Scope

Global Scope

Accessible throughout script Accessible within a function Access global variables within a function

Local Scope

GLOBAL keyword

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Global keyword
Example: $cfgHost = localhost; function getHostName() {

global $cfgHost;
$label = The hostname is: ; // local scope. echo $label . $cfgHost; }

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Inserting External Files


include([<path>]<filename>); include_once([<path>]<filename>); require([<path>]<filename>); require_once([<path>]<filename>);

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Day 4

Introduction to Database & MySQL Using MySQL from PHP Putting things Together Lab Project

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Database

A structured collection of data Contains 1 or more Tables

db1
db1.table_1 db1.table_3

db1.table_2

db1.table_4

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Table

Tables contain records Records contain fields Fields have values

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Creating a Database
Syntax: CREATE DATABASE dbname; Example:

CREATE DATABASE training;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Using a Database
Syntax: USE dbname; Example:

USE training;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Creating a Table
Syntax: CREATE TABLE table_name ( field1_name field_spec,

field2_name
); Example:

field_spec

CREATE TABLE student (

st_id

bigint primary key auto_increment,

st_fname varchar(100), st_age );


9 - 12 April 2007

tinyint unsigned
Trainer: Shaizar Md Daud (jat@shaizar.com)

Querying a Table
Syntax: SELECT <field_list> FROM <table_name> [ WHERE <condition> ];

Example:
SELECT * FROM student; SELECT * FROM student WHERE st_age > 20;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Inserting a Record
Syntax: INSERT INTO `table_name` (<fieldlist>) VALUES (<valuelist>};

Example:
INSERT INTO student (st_fname, st_age) VALUES ( Ahmad Albab, 20 );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Updating a Record
Syntax: UPDATE table_name SET <field value list> [ WHERE <condition> ];

Example:
UPDATE student SET st_fname = Jat, st_age = 22 WHERE st_id = 1;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Deleting a Record or Records


Syntax: DELETE FROM table_name [ WHERE <condition> ]; Example:

DELETE FROM student WHERE st_id = 2;


DELETE FROM student WHERE st_age < 18;

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

PHP Functions for MySQL


PHP Function mysql_connect() mysql_select_db() mysql_query() mysql_fetch_array() mysql_insert_id() mysql_error() Description Connect to a MySQL database server Make a database active Execute SQL statement on active database Fetch a row from query result and return as data as an array of fields and values Returns the last inserted ID from an auto_increment field Returns the last MySQL error message.

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

mysql_connect()
Syntax: resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]]); Example:

$dbh = mysql_connect( localhost, root, );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

mysql_select_db()
Syntax: bool mysql_select_db ( string database_name [, resource link_identifier] ) Example: $stat = mysql_select_db( training, $dbh );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

mysql_query()
Syntax: resource mysql_query ( string query [, resource link_identifier] ) Example: $rh = mysql_query( SELECT * FROM student, $dbh );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

mysql_fetch_array()
Syntax: array mysql_fetch_array ( resource result [, int result_type] ) Example: $row = mysql_fetch_array( $rh );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

mysql_insert_id()
Syntax: int mysql_insert_id( [resource link_identifier] )

Example:
$newId = mysql_insert_id( $dbh );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

mysql_error()
Syntax: string mysql_error( [resource link_identifier] ) Example:

$errMsg = mysql_error( $dbh );

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Putting It All Together

Lab Project

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Thank You

Shaizar Md Daud @ Jat jat@shaizar.com Tel: 012-311 2506

9 - 12 April 2007

Trainer: Shaizar Md Daud (jat@shaizar.com)

Вам также может понравиться