PHP MySQL Basic - Training Slides
PHP MySQL Basic - Training Slides
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
9 - 12 April 2007
What is HTML?
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
Single Tags
9 - 12 April 2007
9 - 12 April 2007
HTML Comments
9 - 12 April 2007
9 - 12 April 2007
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
9 - 12 April 2007
9 - 12 April 2007
<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
9 - 12 April 2007
Example: <ol>
<li>Item 1</li> <li>Item 2</li> </ol>
9 - 12 April 2007
Example: <UL>
<li>Item 1</li> <li>Item 2</li> </UL>
9 - 12 April 2007
HTML Table
Start Tag <table> <tr> <td> End Tag </table> </tr> </td> Description Table Container Table Row Table Data
9 - 12 April 2007
9 - 12 April 2007
<a id=myId>
</a>
9 - 12 April 2007
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
Form Tag
Syntax: <FORM action=<url> method=[ GET | POST ]> Example: <FORM action=register.php method=post> . . . . . . </FORM>
9 - 12 April 2007
Input Tag
Attributes
type
Select Tag
Attributes
9 - 12 April 2007
Option Tag
value selected
9 - 12 April 2007
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
<OPTION value=joh>Johor</OPTION>
<OPTION value=ked selected>Kedah</OPTION> </SELECT>
9 - 12 April 2007
TextArea Tag
Attributes
rows cols wrap: [ soft | hard | off ] Text to be displayed in the form field.
CDATA
9 - 12 April 2007
9 - 12 April 2007
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
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
9 - 12 April 2007
<?php
echo Hello from PHP; PHP Section ?> </body> HTML Section </html>
9 - 12 April 2007
Language Syntax
PHP Statement
9 - 12 April 2007
PHP Comments
9 - 12 April 2007
Day 2
9 - 12 April 2007
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
Examples
9 - 12 April 2007
Arrays
Stores multiple values in a single variable Stored values can be of different types Index-based or Key-based
9 - 12 April 2007
Index-based Array
9 - 12 April 2007
Associate Array
9 - 12 April 2007
Constants
9 - 12 April 2007
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
9 - 12 April 2007
Flow Control
9 - 12 April 2007
IF Statement
Syntax: if ( <condition> ) { [ block to be executed if <condition> is true ]
9 - 12 April 2007
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
IF ELSE IF
Syntax: if ( <condition1> ) { [ block to be executed if <condition1> is true ]
9 - 12 April 2007
Nested IF
Syntax: if ( <condition1> ) { [ block to be executed if <condition1> is true ]
9 - 12 April 2007
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
9 - 12 April 2007
WHILE Loop
Syntax: while ( <condition> ) { [ block to be executed
9 - 12 April 2007
9 - 12 April 2007
FOR Loop
Syntax: for ( <init>; <condition>; <iterator> ) { [ block to be executed
9 - 12 April 2007
9 - 12 April 2007
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
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
Using Functions
Syntax: function_name([<param>]) Example:
echo date();
echo date( d-m-Y ); $a = sprintf( hello );
9 - 12 April 2007
User-defined functions
Syntax: function function_name() { }
9 - 12 April 2007
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
Global keyword
Example: $cfgHost = localhost; function getHostName() {
global $cfgHost;
$label = The hostname is: ; // local scope. echo $label . $cfgHost; }
9 - 12 April 2007
9 - 12 April 2007
Day 4
Introduction to Database & MySQL Using MySQL from PHP Putting things Together Lab Project
9 - 12 April 2007
Database
db1
db1.table_1 db1.table_3
db1.table_2
db1.table_4
9 - 12 April 2007
Table
9 - 12 April 2007
Creating a Database
Syntax: CREATE DATABASE dbname; Example:
9 - 12 April 2007
Using a Database
Syntax: USE dbname; Example:
USE training;
9 - 12 April 2007
Creating a Table
Syntax: CREATE TABLE table_name ( field1_name field_spec,
field2_name
); Example:
field_spec
st_id
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
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
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
9 - 12 April 2007
9 - 12 April 2007
mysql_connect()
Syntax: resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]]); Example:
9 - 12 April 2007
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
mysql_query()
Syntax: resource mysql_query ( string query [, resource link_identifier] ) Example: $rh = mysql_query( SELECT * FROM student, $dbh );
9 - 12 April 2007
mysql_fetch_array()
Syntax: array mysql_fetch_array ( resource result [, int result_type] ) Example: $row = mysql_fetch_array( $rh );
9 - 12 April 2007
mysql_insert_id()
Syntax: int mysql_insert_id( [resource link_identifier] )
Example:
$newId = mysql_insert_id( $dbh );
9 - 12 April 2007
mysql_error()
Syntax: string mysql_error( [resource link_identifier] ) Example:
9 - 12 April 2007
Lab Project
9 - 12 April 2007
Thank You
9 - 12 April 2007