+ All Categories
Home > Documents > PHP MySQL Basic - Training Slides.ppt

PHP MySQL Basic - Training Slides.ppt

Date post: 12-Apr-2015
Category:
Upload: basyiroh-saad
View: 451 times
Download: 6 times
Share this document with a friend
Description:
php training
81
Introduction to PHP & MySQL A 4 days Course Presented by Shaizar Md Daud @ Jat
Transcript
Page 1: PHP MySQL Basic - Training Slides.ppt

Introduction to PHP & MySQL

A 4 days Course

Presented by

Shaizar Md Daud @ Jat

Page 2: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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

Page 3: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Day 1

Introduction to HTML Introduction to PHP PHP - The Language

Page 4: PHP MySQL Basic - Training Slides.ppt

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

Page 5: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Markup Tags

Single Tags– No Content or Child Tags

Syntax:

<tagname/> or <tagname>

Example:

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

Syntax:

<tagname/> or <tagname>

Example:

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

Page 6: PHP MySQL Basic - Training Slides.ppt

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:

<tagname>Some Content</tagname>

Example:

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

Syntax:

<tagname>Some Content</tagname>

Example:

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

Page 7: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Comments

Comments are not rendered (displayed)

Syntax:

<!-- comment text -->

Example:

<!--

This is a HTML comment

-->

Syntax:

<!-- comment text -->

Example:

<!--

This is a HTML comment

-->

Page 8: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Document Structure

Document Type Declaration

HTML Container– HTML Header– HTML Body

Page 9: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

DOCTYPEHTML 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">

Page 10: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Container TagsHTML Document Structure

Start Tag End Tag Description

<html> </html> HTML Container

<head> </head> HTML Header Section

<body> </body> HTML Body Section

Page 11: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Layout Tags

Start Tag End Tag Description

<p> </p> Paragraph Text

<br/> - Insert Line Break

<div> </div> Division Block

Page 12: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Text Formatting Tags

Start Tag End Tag Description

<b> </b> Bold Text

<strong> </strong> Bold Text

<i> </i> Italic Text

<u> </u> Underline Text

<hr/> - Horizontal Line

<hn> </hn> Text Headers [ n = 1 – 6 ]

<span> </span> Apply Format Elements to Text

Page 13: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML List Tags

Start Tag End Tag Description

<ol> </ol> Ordered List

<ul> </ul> Unordered List

<li> </li> List Item

Page 14: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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>

Syntax:

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

Example:<ol>

<li>Item 1</li><li>Item 2</li>

</ol>

Page 15: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Unordered List Tag

Syntax:

<UL type=“[ DISC | CIRCLE | SQUARE ]”>[ list items ]</UL>

Example:<UL>

<li>Item 1</li><li>Item 2</li>

</UL>

Syntax:

<UL type=“[ DISC | CIRCLE | SQUARE ]”>[ list items ]</UL>

Example:<UL>

<li>Item 1</li><li>Item 2</li>

</UL>

Page 16: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Table

Start Tag End Tag Description

<table> </table> Table Container

<tr> </tr> Table Row

<td> </td> Table Data

Page 17: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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>

<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>

Page 18: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML TagsHTML Links

Start Tag End Tag Description

<a href=“[url]”> </a> Anchor link to a URL.

<a href=“#myId”> </a> Anchor link to an Target ID.

<a id=“myId”> </a> Target ID to link to.

Page 19: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

HTML Forms

Start Tag End Tag Description

<form> </form> Form Container

<input /> - Input Fields

<select> </select> Select Box Container

<option> </option> Select Box Options

<textarea> </textarea> Text Box

Page 20: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Form Tag

Syntax:

<FORM action=“<url>” method=“[ GET | POST ]”>

Example:<FORM action=“register.php” method=“post”>

. . .

. . .</FORM>

Syntax:

<FORM action=“<url>” method=“[ GET | POST ]”>

Example:<FORM action=“register.php” method=“post”>

. . .

. . .</FORM>

Page 21: PHP MySQL Basic - Training Slides.ppt

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

Page 22: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Input Tag, cont’d

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”/>

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”/>

Page 23: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Select Tag

Attributes– name– size– multiple

Page 24: PHP MySQL Basic - Training Slides.ppt

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

Page 25: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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>

Example:

<SELECT name=“addr_state” size=“1”>

<OPTION value=“sel” selected>Selangor</OPTION>

<OPTION value=“joh”>Johor</OPTION>

<OPTION value=“ked”>Kedah</OPTION>

</SELECT>

Page 26: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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>

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>

Page 27: PHP MySQL Basic - Training Slides.ppt

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.

Page 28: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

TextArea Tag, cont’d

Example:

<TEXTAREA name=“notes” rows=“10” cols=“40”>

Text displayed in TextArea form field.

It supports multiple text lines.

</TEXTAREA>

Example:

<TEXTAREA name=“notes” rows=“10” cols=“40”>

Text displayed in TextArea form field.

It supports multiple text lines.

</TEXTAREA>

Page 29: PHP MySQL Basic - Training Slides.ppt

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

Page 30: PHP MySQL Basic - Training Slides.ppt

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

– Con Powerful hardware High server resource High Network resource

Client-side– Pro

Better response Richer user interface No or lighter server

requirements

– Con Less secure Client dependent Challenging

Deployment

Page 31: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

PHP Basic Construct

<html>

<body>

<h1>HTML Title</h1>

<?php

echo “Hello from PHP”;

?>

</body>

</html>

<html>

<body>

<h1>HTML Title</h1>

<?php

echo “Hello from PHP”;

?>

</body>

</html>

HTML Section

PHP Section

HTML Section

Page 32: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Language Syntax

Start PHP Section– <?php or <?

End PHP Section– ?>

PHP Statement– A_php_statement;

Page 33: PHP MySQL Basic - Training Slides.ppt

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

Page 34: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Day 2

Using Variables Giving Your Code Life – Flow Control

Page 35: PHP MySQL Basic - Training Slides.ppt

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 0.4, 1.234

Boolean True, false, 0, <none zero>

Array

Object

Resource Database, file

Page 36: PHP MySQL Basic - Training Slides.ppt

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

Page 37: PHP MySQL Basic - Training Slides.ppt

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();

Page 38: PHP MySQL Basic - Training Slides.ppt

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

Page 39: PHP MySQL Basic - Training Slides.ppt

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

Page 40: PHP MySQL Basic - Training Slides.ppt

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”;

Page 41: PHP MySQL Basic - Training Slides.ppt

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;

Page 42: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Operators

Operator Description

+ Addition

++ Increment

- Subtraction

-- Decrement

* Multiply

/ Division

% Modulo

. Concatenate (string)

Operator Description

= Assignment

== Equality

=== Exact Equality

! Not

&& Logical AND

|| Logical OR

>, >= Greater (or Equal)

<, <= Less (or Equal)

Page 43: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Form Variables

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

Page 44: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Flow Control

Conditional Statements Loops

Page 45: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

IF Statement

Syntax:

if ( <condition> ) {

[ block to be executed if <condition> is true ]

}

Syntax:

if ( <condition> ) {

[ block to be executed if <condition> is true ]

}

Page 46: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

IF … ELSE

Syntax:

if ( <condition> ) {

[ block to be executed if <condition> is true ]

}

else {

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

}

Syntax:

if ( <condition> ) {

[ block to be executed if <condition> is true ]

}

else {

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

}

Page 47: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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 ]}

Syntax:

if ( <condition1> ) {

[ block to be executed if <condition1> is true ]

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

Page 48: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Nested IF

Syntax:

if ( <condition1> ) {

[ block to be executed if <condition1> is true ]

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

Syntax:

if ( <condition1> ) {

[ block to be executed if <condition1> is true ]

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

Page 49: PHP MySQL Basic - Training Slides.ppt

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”;}

Page 50: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Loops

WHILE loop FOR loop FOREACH loop

Page 51: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

WHILE Loop

Syntax:

while ( <condition> ) {

[ block to be executed

while <condition> is true ]

}

Syntax:

while ( <condition> ) {

[ block to be executed

while <condition> is true ]

}

Page 52: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

WHILE Loop Example

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

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

Page 53: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

FOR Loop

Syntax:

for ( <init>; <condition>; <iterator> ) {

[ block to be executed

while <condition> is true ]

}

Syntax:

for ( <init>; <condition>; <iterator> ) {

[ block to be executed

while <condition> is true ]

}

Page 54: PHP MySQL Basic - Training Slides.ppt

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 ] . ‘<br />’;}

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

Page 55: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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 ]}

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 ]}

Page 56: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

FOREACH Loop Example

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

Example 2:foreach ( $profile as $key => $item ) { print “Item “ . $key . “: “ . $item . ‘<br />’;}

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

Example 2:foreach ( $profile as $key => $item ) { print “Item “ . $key . “: “ . $item . ‘<br />’;}

Page 57: PHP MySQL Basic - Training Slides.ppt

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!

Page 58: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Using Functions

Syntax:

function_name([<param>])

Example:

echo date();

echo date( ‘d-m-Y’ );

$a = sprintf( ‘hello’ );

Syntax:

function_name([<param>])

Example:

echo date();

echo date( ‘d-m-Y’ );

$a = sprintf( ‘hello’ );

Page 59: PHP MySQL Basic - Training Slides.ppt

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;

}

Syntax:

function function_name() {

}

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

return $rv;

}

Page 60: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Variable Scope

Global Scope– Accessible throughout script

Local Scope– Accessible within a function

GLOBAL keyword– Access global variables within a function

Page 61: PHP MySQL Basic - Training Slides.ppt

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;

}

Example:

$cfgHost = ‘localhost’;

function getHostName() {

global $cfgHost;

$label = ‘The hostname is: ‘; // local scope.

echo $label . $cfgHost;

}

Page 62: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Inserting External Files

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

Page 63: PHP MySQL Basic - Training Slides.ppt

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

Page 64: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Database

A structured collection of data

Contains 1 or more Tables

db1

db1.table_1

db1.table_2

db1.table_3

db1.table_4

Page 65: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Table

Tables contain records Records contain fields Fields have values

Page 66: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Creating a Database

Syntax:

CREATE DATABASE dbname;

Example:

CREATE DATABASE training;

Syntax:

CREATE DATABASE dbname;

Example:

CREATE DATABASE training;

Page 67: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Using a Database

Syntax:

USE dbname;

Example:

USE training;

Syntax:

USE dbname;

Example:

USE training;

Page 68: PHP MySQL Basic - Training Slides.ppt

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

);

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

);

Page 69: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Querying a Table

Syntax:

SELECT <field_list> FROM <table_name> [ WHERE <condition> ];

Example:

SELECT * FROM student;

SELECT * FROM student WHERE st_age > 20;

Syntax:

SELECT <field_list> FROM <table_name> [ WHERE <condition> ];

Example:

SELECT * FROM student;

SELECT * FROM student WHERE st_age > 20;

Page 70: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Inserting a Record

Syntax:

INSERT INTO `table_name` (<fieldlist>) VALUES (<valuelist>};

Example:

INSERT INTO student (st_fname, st_age) VALUES ( ‘Ahmad Albab’, 20 );

Syntax:

INSERT INTO `table_name` (<fieldlist>) VALUES (<valuelist>};

Example:

INSERT INTO student (st_fname, st_age) VALUES ( ‘Ahmad Albab’, 20 );

Page 71: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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;

Syntax:

UPDATE table_name SET <field value list> [ WHERE <condition> ];

Example:

UPDATE student SET st_fname = ‘Jat’, st_age = 22 WHERE st_id = 1;

Page 72: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

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;

Syntax:

DELETE FROM table_name [ WHERE <condition> ];

Example:

DELETE FROM student WHERE st_id = 2;

DELETE FROM student WHERE st_age < 18;

Page 73: PHP MySQL Basic - Training Slides.ppt

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.

Page 74: PHP MySQL Basic - Training Slides.ppt

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’, ‘’ );

Syntax:

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

Example:

$dbh = mysql_connect( ‘localhost’, ‘root’, ‘’ );

Page 75: PHP MySQL Basic - Training Slides.ppt

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 );

Syntax:

bool mysql_select_db ( string database_name [, resource link_identifier] )

Example:

$stat = mysql_select_db( ‘training’, $dbh );

Page 76: PHP MySQL Basic - Training Slides.ppt

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 );

Syntax:

resource mysql_query ( string query [, resource link_identifier] )

Example:

$rh = mysql_query( ‘SELECT * FROM student’, $dbh );

Page 77: PHP MySQL Basic - Training Slides.ppt

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 );

Syntax:

array mysql_fetch_array ( resource result [, int result_type] )

Example:

$row = mysql_fetch_array( $rh );

Page 78: PHP MySQL Basic - Training Slides.ppt

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 );

Syntax:

int mysql_insert_id( [resource link_identifier] )

Example:

$newId = mysql_insert_id( $dbh );

Page 79: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

mysql_error()

Syntax:

string mysql_error( [resource link_identifier] )

Example:

$errMsg = mysql_error( $dbh );

Syntax:

string mysql_error( [resource link_identifier] )

Example:

$errMsg = mysql_error( $dbh );

Page 80: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Putting It All Together

Lab Project

Page 81: PHP MySQL Basic - Training Slides.ppt

9 - 12 April 2007 Trainer: Shaizar Md Daud ([email protected])

Thank You

Shaizar Md Daud @ Jat [email protected] Tel: 012-311 2506


Recommended