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 Don‟t! Introduction to Database & MySQL Using MySQL from PHP Putting things Together – Lab Project 9 - 12 April 2007
Trainer: Shaizar Md Daud (
[email protected] )
Day 1
Introduction to HTML Introduction to PHP PHP - The Language
9 - 12 April 2007
Trainer: Shaizar Md Daud (
[email protected] )
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
9 - 12 April 2007
Trainer: Shaizar Md Daud (
[email protected] )
HTML Markup Tags
Single Tags –
No Content or Child Tags
Syntax:
or
Example:
, ,
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Markup Tags, cont’d
Start and End Tags –
Contains Content or Child Tags
Syntax: Some Content Example:
The quick brown fox jumps over the lazy dog
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Comments
Comments are not rendered (displayed)
Syntax: Example:
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Document Structure
Document Type Declaration HTML Container – –
HTML Header HTML Body
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
DOCTYPE HTML Document Structure
Common DOCTYPEs:
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Container Tags HTML Document Structure Start Tag
End Tag
Description
HTML Container
HTML Header Section
HTML Body Section
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Layout Tags Start Tag
End Tag
-
9 - 12 April 2007
Description Paragraph Text Insert Line Break Division Block
Trainer: Shaizar Md Daud ([email protected] )
HTML Text Formatting Tags Start Tag
End Tag
Bold Text
Bold Text
Italic Text
Underline Text
-
Horizontal Line
9 - 12 April 2007
Description
Text Headers [ n = 1 – 6 ] Apply Format Elements to Text Trainer: Shaizar Md Daud ([email protected] )
HTML List Tags Start Tag
End Tag
Description
Ordered List
Unordered List
List Item
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Ordered List Tag Syntax: [ list items ]
Example: Item 1 Item 2
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Unordered List Tag Syntax:
Example:
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Table Start Tag
End Tag
Description
Table Container
Table Row
Table Data
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Table Example Row 1, Column 1 Row 1, Column 2 Row 2, Column 1 Row 2, Column 2
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
HTML Tags HTML Links Start Tag
End Tag
Anchor link to a URL.
Anchor link to an Target ID.
Target ID to link to.
9 - 12 April 2007
Description
Trainer: Shaizar Md Daud ([email protected] )
HTML Forms Start Tag
End Tag
Description
Form Container
-
Select Box Container
Select Box Options
9 - 12 April 2007
Input Fields
Text Box
Trainer: Shaizar Md Daud ([email protected] )
Form Tag Syntax:
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
Input Tag, cont’d Example:
Male Female Yes 9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Select Tag
Attributes – – –
name size multiple
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Option Tag
Used within SELECT tags. Defines SELECT Items. Attributes – –
value selected
CDATA = Select item label
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
SELECT … OPTION Example: Selangor
Johor Kedah
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
SELECT … OPTION (multiple) Example: Selangor
Johor Kedah
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
TextArea Tag
Attributes – – –
rows cols wrap: [ soft | hard | off ]
CDATA –
Text to be displayed in the form field.
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
TextArea Tag, cont’d Example:
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
Server-side vs Client-side
Server-side –
Pro
–
–
More secure Client independent Light-weight Client Easier deployment
Powerful hardware High server resource High Network resource
9 - 12 April 2007
Pro
–
Con
Client-side Better response Richer user interface No or lighter server requirements
Con
Less secure Client dependent Challenging Deployment Trainer: Shaizar Md Daud ([email protected] )
PHP Basic Construct HTML Section
HTML Title
?>
HTML Section
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Language Syntax
Start PHP Section –
End PHP Section –
PHP Statement –
A_php_statement;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
Day 2
Using Variables Giving Your Code Life – Flow Control
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Data Types Data Type
Description
String
“text”, „text‟
Integer
-2, -1, 0, 1, 2
Float Boolean
0.4, 1.234 True, false, 0,
Array Object Resource
Database, file 9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
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 ([email protected] )
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 ([email protected] )
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 ([email protected] )
Constants
Value is fixed once created Example – Creating constants – –
define(“MYCONST”, 23); define(“MYCONSTSTR”, “My String”);
Example – Using constants –
print “The value of MYCONST:” . MYCONST;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Operators Operator
Description
Operator
=
Assignment
+
Addition
==
Equality
++
Increment
===
Exact Equality
-
Subtraction
Not
--
Decrement
&&
Logical AND
*
Multiply
||
Logical OR
/
Division
%
Modulo
!
>, >=
Greater (or Equal)
<, <=
Less (or Equal)
9 - 12 April 2007
.
Description
Concatenate (string) Trainer: Shaizar Md Daud ([email protected] )
Form Variables
$_GET[] $_POST[] $_REQUEST[]
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Flow Control
Conditional Statements Loops
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
IF Statement Syntax: if ( ) { [ block to be executed if is true ]
}
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
IF … ELSE Syntax: if ( ) { [ block to be executed if is true ]
} else { [ block to be executed if is false ] }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
IF … ELSE IF Syntax: if ( ) { [ block to be executed if is true ]
} else if ( ) { [ block to be executed if is true ] }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Nested IF Syntax: if ( ) { [ block to be executed if is true ]
if ( ) { [ block to be executed if is true ] } [ continue ] }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
Loops
WHILE loop FOR loop FOREACH loop
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
WHILE Loop Syntax: while ( ) { [ block to be executed
while is true ] }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
WHILE Loop Example Example: $i = 0; while( $i print . . $i++; }
< sizeof( $profile ) ) { “Item $i: “ $profile[ $i ] „ ‟;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
FOR Loop Syntax: for ( ; ; ) { [ block to be executed
while is true ] }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
FOR Loop Example Example: for ( $i = 0; $i < sizeof( $profile ); $i++ ) { print “Item $i: “ . $profile[ $i ] . „ ‟; }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
FOREACH Loop Syntax 1: foreach ( as ) { [ block to be executed for each item in array ]
} Syntax 2: foreach ( as => ) { [ block to be executed for each item in array ] }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
FOREACH Loop Example Example 1: foreach ( print . . }
$profile as $item ) { “Item: “ $item „ ‟;
Example 2: foreach ( print . . }
$profile as $key => $item ) { “Item “ . $key . “: “ $item „ ‟;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Day 3
Using Functions Creating Custom Functions Making Things Constant Variable Scope – Now You See Me, Now You Don‟t!
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Using Functions Syntax: function_name([ ]) Example:
echo date(); echo date( ‘d-m-Y’ ); $a = sprintf( ‘hello’ );
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
User-defined functions Syntax: function function_name() { }
function function_name($param1, $param2=2) { return $rv; }
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Variable Scope
Global Scope –
Local Scope –
Accessible throughout script Accessible within a function
GLOBAL keyword –
Access global variables within a function
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
Inserting External Files
include([]); include_once([]); require([]); require_once([]);
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Day 4
Introduction to Database & MySQL Using MySQL from PHP Putting things Together – Lab Project
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Database
A structured collection of data Contains 1 or more Tables
9 - 12 April 2007
db1 db1.table_1
db1.table_3
db1.table_2
db1.table_4
Trainer: Shaizar Md Daud ([email protected] )
Table
Tables contain records Records contain fields Fields have values
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Creating a Database Syntax: CREATE DATABASE dbname; Example:
CREATE DATABASE training;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Using a Database Syntax: USE dbname; Example:
USE training;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Creating a Table Syntax: CREATE TABLE table_name ( field1_name
field_spec,
field2_name
field_spec
); Example: CREATE TABLE student (
st_id
bigint primary key auto_increment,
st_fname varchar(100), st_age
tinyint unsigned
); 9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Querying a Table Syntax: SELECT FROM [ WHERE ];
Example: SELECT * FROM student; SELECT * FROM student WHERE st_age > 20;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Inserting a Record Syntax: INSERT INTO `table_name` () VALUES (};
Example: INSERT INTO student (st_fname, st_age) VALUES ( „Ahmad Albab‟, 20 );
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Updating a Record Syntax: UPDATE table_name SET [ WHERE ];
Example: UPDATE student SET st_fname = „Jat‟, st_age = 22 WHERE st_id = 1;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Deleting a Record or Records Syntax: DELETE FROM table_name [ WHERE ]; Example:
DELETE FROM student WHERE st_id = 2; DELETE FROM student WHERE st_age < 18;
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
PHP Functions for MySQL PHP Function
Description
mysql_connect()
Connect to a MySQL database server
mysql_select_db()
Make a database active
mysql_query()
Execute SQL statement on active database
mysql_fetch_array()
Fetch a row from query result and return as data as an array of fields and values
mysql_insert_id()
Returns the last inserted ID from an auto_increment field
mysql_error()
Returns the last MySQL error message.
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
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 ([email protected] )
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 ([email protected] )
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 ([email protected] )
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 ([email protected] )
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 ([email protected] )
mysql_error() Syntax: string mysql_error( [resource link_identifier] ) Example:
$errMsg = mysql_error( $dbh );
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Putting It All Together
Lab Project
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )
Thank You
Shaizar Md Daud @ Jat [email protected] Tel: 012-311 2506
9 - 12 April 2007
Trainer: Shaizar Md Daud ([email protected] )