Professional Documents
Culture Documents
Introduction PHP
History
PHP/FI 2
enAkgqaM 1997 Rasmus bnkarGPivDn_nvU PHP/FI 2 rhUtmkdl;Ex vicki a qaMdEdlbnab;BI Andi Gutmans
ehIynig Zeev Suraski )anRbTHeXIjnUv PHP/FI cMeBlEdlBYkeKkMBugEsVgrknUvPasaedIm,IGPivDn_nUvKMeragbegIt
E-commerce solution sMrab;sklviTaly rbs;Bk
Y eK . BYkeK)aneGaydwgfa PHP/FI BMuTan;manlTPaB
RKb;RKan;enAeLIyehIyxVHxatnUv lkNBiessCaeRcIn . cMncu mYyEdlmankarcab;GarmN_CageK KW while loops
EdlBYkeKnwgRtUvRbtibti .
PHP 3>
Zeev and Andi sMerccits
resr scripting language eLIgvijb:uEnBvMu tman RasmuscUlrYmedIm,IGPivDn_nvU
PHP3eLIyehIy)anpln; vU eQaHff
I aHypertext PreprocessoredIm,IbBaak;fa PHPKWCaplitplepSgmYyeTotehIynwg
minRtwmEtykmkeRbIsMrab;Etkargarpal; xnenaHeT . Zeev and Andi k)anbegItnUv Extension API Edl API
begtI fIenHvamanlTPaBbMeBjnUvkargarCaeRcIndUcCa Accessing databases , spell checkers ehIynwg beck
viTadTeTot EdleFVeI Gaymankarcab;GarmN_BsI MNak;GkGPivDn_ CaeRcInmkcUlrYmkgKMerag PHP.
enAxNeBlenaHEdr PHP kRtUv)anbeBjjnUvCMnan;fIrbs;xnKW PHP 3 enA fTI 3 Ex mifuna qaM 1998
Edltamkar)a:n;san PHP nwgRtUv)antMeLIg RbmaN Cag 50 000 domains b:uEnCamYytYelxBitR)akd elIkdMbUgrbs;
PHP RtUv)aneKtMeLIgeRcInCagmYylan Domain eTAeTot .
Page 1
PHP 4
>
Page 2
Building Block
>
VariableKWCaRKwHdsMxan;sMrab;PasakMuBTr vaRtUv)aneKeRbIedIm,IpknUvtMlCabeNaHGasnenAeBlEdlRbtibtikar
script mg . variable RtUv)anpl;tMleTAeGay enAeBlEdl Script cab;epImtMeNIrkar b enAeBlGkeRbIR)as;bBal
$sok ;
$sok_som_neang ;
$_Dara ;
$Chenda22 ;
rbs;
PHP
$num1 = 5;
$num2 = 8;
karRbkas
variable
Operator and
dUcxageRkam
vamantMlesnI gw kareRbI print 8; dUcecHmannyfa
$num2 pktMm 8
cUrcgcaMfaral;cgu bBab;nr)aykarN_rbs; PHP nimYyRtUvEtbBab;edaysBaa ( ; ) semicolon.
2 Data Types >
RbePTxusKanTinnyRtUv)aneRbIR)as;nvU cMnYnsrubrbs;GgcgcaMepSgKa ehIyvaGacRbRBwte TAenAeBlGk
erobcMvaenAkg script rbs;Gk . manPasakmviFmI YycMnnY TamTareGayGksresrkmviFI RbkasnUvRbePT nTinny
print $num2;
Page 3
RbePT
]TahrN_
BiBNna
pknUvtMlelxCacMnYnKt;
Integer
Double
3.234
String
"hello"
pknUvtMlelxCacMnYnTsPaK
pknUvtMlCabnM ntYGkSr
Boolean
True
Array
emeronTI
Object
emeronTI
]TahrN_ 1
Gettype.php
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
<html>
<head><titile>Display Data Type</title>
</head>
<body>
<h2>Using gettype</h2>
<?php
$testing;
Print gettype($testing);// NULL
$testing = 5;
print gettype($testing); // integer
print "<br />";
$testing = "five";
print gettype($testing); // string
print "<br />";
$testing = 5.0;
print gettype($testing); // double
print "<br />";
$testing = true;
print gettype($testing); // boolean
print "<br />";
?>
Page 4
25:
26:
27:
</body>
</html>
$name = "Rithya";
>
RbsinebIGkeRbIR)as;nUv
PHP engine
>
variable
minRtUv)anCMnYsedaytMlenaHeT .
>
]TahrN_ 2
Quotation.php
01:
<html>
02:
<head><title>Single Quotation and Double Quotation mark</title>
03:
</head>
04:
<body>
05:
<h2> Using double quatation and Signle quatation mark</h2>
06:
07:
<?
08:
$name = "Rithya";
09:
print "Hello,$name<br/>"; // Hello,Rithya
10:
11:
12:
13:
14:
</body>
</html>
Page 5
>
gettype() KWCa function EdleRbIedImI,TTYlnUvRbePT variable edayELk var_dump()
eRbIedIm,IR)ab;nvU RbePT variable nig cMnHu rbs;va . eRcInCagenHeTAeTot sMrab;RbePTtMlEdlsKsajdUcCa
arrays nig object
var_dump() pl;eGaynUvBtmanRKb;RbePTEdlmanenAkgVariable enaH .
]TahrN_ 3
3.Displaying Type Information with var_dump()
Var_dump.php
01:
<html>
02:
<head>
03:
<title>Displaying Type Information with var_dump</title>
04:
</head>
05:
<body>
06:
<h2>using Var_dump</h2>
07:
08:
<?php
09:
$testing=5;
10:
print var_dump($testing);
11:
?>
12:
13:
</body>
14:
</html>
4.The Cast Operators
Operator
Changes Type To
(int), (integer)
Integer
Floating point
(string)
String
(bool), (Boolean)
Boolean
(array)
Array
(object)
Object
]TahrN_ 5
Casting.php
<html>
<head>
<title>Casting variable</title>
</head>
<body>
<h2>Using Casting to Changing Type</h2>
<?php
$unexpect=3.14;
Developing web Application with PHP
Page 6
$holder = (double)$unexpect;
print gettype($holder);
print "--- $holder<br/>";
$holder = (string)$unexpect;
print Gettype($holder);
print "-- $holder<br/>";
$holder = (integer)$unexpect;
print gettype($holder);
print "-- $holder<br/>";
$holder = (Double)$unexpect;
print gettype($holder);
print "---- $holder<br/>";
$holder = (boolean)$unexpect;
print gettype($holder);
print "-- $holder<br/>";
?>
</body>
</html>
>
Operators
KWCanimti sBaaTaMLayNaEdlGkGaceRbItMlmYy b eRcIn begtI ecjCatMlfImyY eTot
ehIytMlEdlRbtibtieday operator enaHRtUv)aneKehAfa operand .
Operand KWCatMlEdlcUlrYmCamYy operator EdlCaTUeTA man operand BIrCamYyOperator mYy .
]TahrN_kareRbI operand BIr CamYy operator edIm,IbegItecjCatMlfmI YyepSgeTot.
4+5>
4 ehIynig 5 KWCa operand EdlRtUv)an operated eday addition operator (+) edIm,IbegtI nUvtMlfI KW 9 . karpMnvU
operand nig operator edIm,Ibegt
I CalTpl RtUv)aneKehAfa expression
5.1 The Assignment Operator >
Assignment operators eRbIedIm,Ipl;nUvtMleTAeGay variable .
dUcEdlGk)anCYbxagelI assignment
operator RtUv)an initialize variable RKb;eBl vaKWCaGkSr ( = ) .Assignment operator cab;yktMlBI operand
xagsaMeTAeGay operand xageqVgdUc]TahrN_
xageRkam
5.Operators and Expressions
$name = DYCHANDOEUN ;
Print $name;
>
Page 7
Name
bUk ( Addition )
Eck ( Division )
KuN ( Multiplication )
yksMNl;BpI lEck ( Modulus )
dk ( Subtraction )
Example
Result
10+5
15
10/3
3.333333333333
10*5
50
10%3
10-2
>
Concatenation operator eRbIedIm,IPab; string sMrab; operator mYyenHvaeFVk
I arCamYyEt string b:ueNaH .
dUecHral; operand EdlminEmnCa string vanwg convert eGayeTACa string sUmBinitemIl]TahrN_
"hello"." world" karsresrEbbenHvaesn
I gw karsresr "hello world" .
$year = 2007; >
Print "Happy khmer new year_".$year ; >
Variable $year EdlCaRbePT integer RtUv)anbMElgeTACa string " 2007 " mun
eBlEdlvaRtUv)anPab;CamYy string Happy khmer new year .
5.4 Combined Assignment Operators >
$x = 4; >
$x = $x + 4; // lTpl $x KW 8
5.3 The Concatenation Operator ( . )
b GkGacCMnYsedaykarsresrdUcxageRkam
$x = 4;
$x += 4; // $
lTpl $x KW 8
Example
Equivalent to
+=
$x += 5
$x = $x + 5
-=
$x -= 5
$x = $x 5
/=
$x /= 5
$x = $x / 5
*=
$x *= 5
$x = $x * 5
Page 8
Example
Equivalent to
%=
$x %= 5
$x = $x % 5
.=
$x .= " test"
$x = $x." test"
>
Comparison operators
eRbIedIm,IeRboeFobnUv
.
]TahrN_edIm,IRtYtBinittMlEdlmanenAkg
operator .
operands
true or false )
$x
nigCatMlEdltUcCagR)aM GkGaceRbICamYynwg
less than
$x < 5
RbsinebI
true
b:uEnRbsinebI
$x
pktMlCaelx 7 enaH
Comparison Operators
Operator
eQaH
==
smmUl
$x == 5
!=
Non-equivalence
$x != 5
===
Identical
$x===5
>
FMCag
tMlxageqVgFMCag tMlxagsaM
$x > 4
>=
FMCag b esI
$x >= 4
<
tUcCag
tMlxageqVgtUcCag tMlxagsaM
$x < 4
($x
tUcCag b esI
tMlxageqVgtUcCag b esItMlxagsaM $x
Operator xagelIenHPaKeRcIneRbICamYy integers or double ehIysMrab;
KWeRbIedIm,IeRbobeFobtMlEdlCa strings .
<=
KW 4)
<= 4
operator(
==
Page 9
>
Logical Operators
Operator Name
Example
Result
||
Or
true
false
||
Or
Or
true
false
||
Xor
Xor
true
true
xor
&&
And
true
false
&&
And
And
true
false
&&
Not
! true
True
true
false
false
false
false
variable
EdlCa
Integer
$x = $x - 1; // $x is decremented.
$x--; // $x is decremented.
$x- = 1; // $x is decremented.
Page 10
Operator Name
Effect on $var
$var++
Post-increment
$var is incremented by 1
++$var
Pre-increment
$var is incremented by 1
$var--
Post-decrement
$var is decremented by 1
--$var
Pre-decrement
$var is decremented by 1
]TahrN_
$num1 = 5;
RtUv)anpl;nUvtMledImrbs; $num1
// lTplEdlnwgRtUvbgajKW tMlrbs; $num1 KW 6
// lTplEdlnwgRtUvbgajKW CatMledImrbs; $num1 KW 5
]TahrN_
$num1 = 5;
$num2 = ++$num1; // pre-increment, $num2
RtUv)anpl;nUvtMlfIrbs; $num1
eTAeGay $num2 .
print $num1; // lTplEdlnwgRtUvbgajKW tMlrbs; $num1 KW 6
print $num2; // lTplEdlnwgRtUvbgajKW CatMlrbs; $num1 KW 6
6.Constants
>
>
Prepare By: DY CHANDOEUN
Page 11
constant
constant.php
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
<html>
<head>
<title>Defining a constant</title>
</head>
<body>
<div>
<?php
define("USER", "Ankor");
print "Welcome ".USER;
?>
</div>
</body>
</html>
]TahrN_ 7
constant2.php
<html>
<head>
<title>Defining a constant</title>
</head>
Page 12
<body>
<div>
<?php
define ("USER", "Angkor",true);
print "Welcome".uSER."<br/>";
print "Welcome".uSeR."<br/>";
print "Welcome".usEr."<br/>";
print "Welcome".uSER."<br/>";
?>
</div>
</body>
</html>
Page 13
1. The if Statement
if(expression)
{
// code
]TahrN_ 8
<Html>
<head>
<title> Using if Statement </title>
</head>
<body>
<h2>Using if statement</h2>
<?php
$user="Thanith";
$pwd="123";
If(($user= ="Thanith" && pwd= ="123"))
print "Login successful";
else
print "Login fail !";
?>
</body>
</Html>
kareRbIR)as; comparasion operator (==) edIm,IeRbobeFob variable $user nig tMlCaGkSr " Thanith "
variable $pwd CamYynwgtMl "123" RbsinebItMlEdlRtUveRbobeFobnigtMlrbs; variable
dUcKaenaH expression nwgplt; M l true ehIy code block nwgRtUvGnuvtn_buE: nRbsinebItMlrbs; $user
breTACa "Romchong" b tMlrbs; $pwd breTACa "124" ehIytMeNIrkar scripteLIgvij enaH
expression EdlenAkg if statement niwgpl;tM l falseehIyCode block nwgminRtUvGnuvtn_ Edl
script nwgEbreTAGnuvtn_nUv else statement
CMnsY vij .
else
print "Login fail !";
Page 14
}
else if ( another expression )
{
}
else
{
// code
RbsinebI expression dMbgU minpl;nUvtMl true enaH block of code dMbgU kminRtUv)an Gnuvtn_Edr else if
cab;epmI eFVIkarCamYy expression rbs;xn RbsinebI expression enHpl;tMm True enaH block kUdTI2 nwgRtUvGnuvtn_
pymkvijkUdEdlsitenAkg else clausenwgRtUvGnuvtn_ CMnYsvij . GkGaceRbIR)as; else if )anCaeRcIneTA tamkarcg
;)anrbs;Gk ehIyRbsinebI Gkmin cg;eGayman Default action eT GkmincaM)ac;eRbIR)as; else clause enaHeT .
>
switch statement KWCaviFIsaRsEdleRbIedIm,Ipas;br lMdab;nkarGnuvtnk
_ Udrbs;kmviFIEdlGaRsyeTAelI
karvaytMlrbs; expression . kareRbIR)as; if statement CamYy else if GkGaceRbIR)as; expression)aneRcIn eday
ELk switch eRbIR)as;Et expression mYyb:ueNaH .karGnuvtn_ code xusKaKWGaRsyeTAelIlTplnexpression
Edlplt; M lCa simple typedUcCa ( number , string , Boolean. l. ) .
2. The switch Statement
switch (expression)
{
case exp:
// execute this if expression results in result1
break;
case exp:
// execute this if expression results in result2
break;
Page 15
default:
//
rbs;
switch statement
]TahrN_ 9
<Html>
<head>
<title>
Using switch Statement
</title>
</head>
<body>
<h2>Using switch statement</h2>
<?php
$name="Daro";
switch($name)
{
case "Dara":
print " Hello Dara";
break;
case "Many":
print "Hello Many";
break;
case "Daro":
print "Hello Daro";
break;
Default:
print "No one know";
}
?>
Developing web Application with PHP
Page 16
</body>
</Html>
3. Loops
>
while ( expression )
{
// do something
}
]TahrN_ 10
while.php
<html>
<head><title>The While Statement</title>
</head>
<body>
<h2>Using the While Statement</h2>
<?php
$sum=0;$i=1;
$str="";
While ($i<=10)
{
$sum+=$i;
$str= $str."$i+";
$i++;
}
echo substr($str,0,-1)."=$sum";
?>
</body>
</html>
>
do..while statement vamanlkNRbhak;RbEhlnwg while statement b:uEnlkNBIrEdlxusKaBI while
statement KW block code rbs; do while statement Gnuvtn_muneBlEdl Expression rbs;vaRtUv)an test nig pl;tMl
true b false .
3.2 The do...while Statement
Page 17
do
{
// code to be executed
}
while (expression);
Test expression
]TahrN_ 11
dowhile.php
<html>
<head><title>The Do While Loop Statement</title>
</head>
<body>
<h2>Using the do while Statement</h2>
</body>
</html>
<?php
$sum=0;$i=1;$str="";
Do
{
$sum=$sum+$i;
$str= $str."$i+";
$i=$i+1;
}
While ($i<=10);
{
$i=$i-1;
echo substr($str,0,-1)."=$sum";
}
?>
3.3 The for Statement
>
<html>
<head>
<title>The for Statement</title>
</head>
<body>
Developing web Application with PHP
Page 18
enAeBlEdl program tMeNIrkardl; for loop variable $i RtUv)an initialize ehIyTest expression
cab;epmI RtYtBiniteTAelI expression rbs;xn RbsinebI expression plt; M lTrue enaH code block nwgRtUvGnuvtn_
bnab;mk $i variable eFVkI arbegIntMlmYyehIy testExpression cab;epImeFVIkarRtYtBinti eTAelI expression
rbs;xn sarCafImg eTot. RbtibtikarenHbnkarGnuvtn_rhUtdl; test expression plt; M l false .
3.4 Breaking Out of Loops with the break Statement
>
test expression
EdlGaceGayGk bBab;va)an b
break.php
01: <html>
02: <head>
03: <title>the break Statement</title>
04: </head>
05: <body>
06: <div>
07:
08: <?php
09:
10: $counter = -5;
11: for ( ; $counter <= 10; $counter++ ) {
12: if ( $counter == 0 ) {
13: break;
14: }
15: $temp = 2000/$counter;
16: print "2000 divided by $counter is.. $temp<br />";
17: }
18: ?>
19:
Developing web Application with PHP
Page 19
20: </div>
21: </body>
22: </html>
01: <!
02: exam continue statement
03: >
04:
<html>
05:
<head>
06:
<title>Using the continue Statement</title>
07:
</head>
08:
<body>
09:
<div>
10:
11:
<?php
12:
13:
$counter = -5;
14:
for( ; $counter <= 10; $counter++ )
15:
{
16:
if ( $counter == 0 )
17:
{
18:
continue;
19:
}
20:
$temp = 2000/$counter;
21:
print "2000 divided by $counter is .. $temp<br />";
22:
}
23:
24:
?>
25:
26:
</div>
27:
</body>
28:
</html>
Page 20
>
]TahrN_ 15
nestingLoop.php
01:
<html>
02:
03:
</head>
04:
<body>
05:
06:
07:
<?php
08:
09:
$j=0;$i=0;
10:
for($i=0;$i<=5;$i++)
11:
12:
13:
for($j=0;$j<=3;$j++)
14:
15:
16:
17:
18:
19:
?>
20:
21:
</body>
22:
</html>
<!
Nesting loops
>
<html>
<head>
Page 21
06:
<title>Nesting Two for Loops</title>
07:
</head>
08:
<body>
09:
<div>
10:
10:
<?php
11:
12:
print "<table border=\"1\">\n";
13:
for ( $y=1; $y<=12; $y++ )
14:
{
15:
print "<tr>\n";
16:
for ( $x=1; $x<=12; $x++ )
17:
{
18:
print "\t<td>";
19:
print ($x*$y);
20:
print "</td>\n";
21:
}
22:
print "</tr>\n";
23:
}
24:
print "</table>";
25:
26:
?>
27:
28:
</div>
29:
30: </body>
31: </html>
Page 22
Function
1.What is Functions ?
$argument2 )
{
//
}
>
$sum+=$i;
$str= $str."$i+";
}
echo substr($str,0,-1)."=$sum";
}
bigsum();
?>
</body>
Page 23
</html>
<html>
<head>
<title>Declaring a Function</title>
</head>
<body>
<?php
function bigsum($count)
{
$sum=0;
for($i=1; $i<=$count; $i++ )
{
$sum+=$i;
$str= $str."$i+";
}
echo substr($str,0,-1)."=$sum";
}
bigsum(10);
?>
</body>
</html>
>
<html>
<head>
<title>A Function That Returns a Value</title>
</head>
<body>
<?php
function addNums($num1,$num2)
{
Page 24
11:
12:
13:
14:
15:
16:
17:
18:
2. Variable Scope
01:
02:
03:
04:
05:
06:
07:
08:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19
20:
<html>
<head>
<title>Local Variable Unavailable Outside a Function</title>
</head>
<body>
<div>
<?php
function test()
{
$testvariable = "this is a test variable";
}
print "test variable: $testvariable<br/>";
?>
</div>
</body>
</html>
Page 25
>
rbs;GkedayBMtu rM vU
<html>
<head>
<title>The global Statement</title>
</head>
<body>
<?php
$lottery=42;
function lotteryToday()
{
global $lottery;
print "Lottery today is $lottery<br />";
}
lotteryToday();
?>
</body>
</html>
Page 26
Array
1. What Is an Array?
Value
Which Element?
Sok
First
Sao
Second
Setha
Third
Mesa
Fourth
Page 27
index
rbs;vaehIyRtUv)andak;enAcenaH
square
>
GkGacbegIt array fI b bEnmtMleTAeGay array )anedaykareRbIR)as;nUv array Identifier .
array identifier KWCabnM n squre brackets nig index rbs;va Caelx b CaeQaH.xageRkamCa ]TahrN_nkarbegIt
$users array edayeRbIvIFIsaRsmYyepSgeTot .
2.2 Defining or Adding to Arrays with the Array Identifier
Page 28
KWCa
array
GkGacbegItnUv
squqre brakets
>
$character["occupation"] = "Programmer";
$character["age"] = 30;
>
>
>
Page 29
>
count() function pl;nUvcMnYnFaturbs; array Edlman bMENkkUdxageRkameyIg)aneRbIR)as; nUv count()function
edIm,IykFatucugeRkayn array $users mkeRbI .
$users = array ("Sambath", "Ratana", "Davy", "Ryda" ); >
print $users[count($users)-1]; >
]TahrN_ 26
5. Getting the Size of an Array
<html>
<body>
<head>
<title>Getting size of array</title>
<body>
<h2>Getting size of array</h2>
<?php
$users[]="Sok";
$users[]="Som";
$users[]="Mom";
$users[]="Mab";
print "<br/>".count($users);
?>
</body>
</html>
array
edaykarpln; vU
index
eTAeGay
array
edayxn Gk
$users[35] ="Sok";
$users[52] ="Som";
$users[890] ="Mom";
$users[52]="Mab";
>>>>>>>>>>
//...
>
Page 30
<html>
<head>
<title>Looping throught array</title>
</head>
<body>
<h2>Looping througth array</h2>
<?php
$users = array ("Sok", "Som", "Mab", "Mab" );
foreach ( $users as $val )
{
print "$val<br />";
}
?>
</body>
</html>
]TahrN_ 27
<html>
<head>
<title>Looping throught array</title>
</head>
<body>
<h2>Looping througth array</h2>
<?php
$users[]="Sok";
$users[]="Som";
$users[]="Mom";
$users[]="Mab";
foreach( $user as $val )
{
print "$val<br/>
}
?>
</body>
</html>
Page 31
>
assosciat array
TaMg
keys
nig
values
GkGaceRbIR)as;CamYynwg
{
//...
>>
KWCa array EdleyIgnwgRtUv loop ehIy $key KWCa variable EdlpknUv key nimYyCabeNaHGasn ehIy
$val KWCa variable EdlpknUv value rbs; array nimYyCabeNaHGasnkg
RKa loop mg .
]ThrN_ 28
$arr
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
<html>
<head>
<title>Looping thought associate array</title>
</head>
<body>
<h2> Looping thought associate array</h2>
<?php
$character = array ("name" => "Sao",
"occupation" => "Student",
"age" => 12,
"Adr" => "Phnom Pehn");
foreach ( $character as $key=>$val )
{
print "$key = $val<br />";
}
?>
</body>
</html>
variable
Edl
Page 32
>
array_merge()
mantYnaTIpMnvU Faturbs; array BIr b array eRcInbBalKa rYcplC; a Array
fmI YyeTotEdlCabnM nFatu array TaMgGs;enaH. ]TahrN_xageRkamKWCa]TahrN_nkarbegtI nUv array BIr KW
array $arr1 nig array $arr2 ehIyeFVIkarbBalKarvagFatun array TaMgBIrrYcplt
; MlTaMgenaHeTAeGay
$arr3 bnab;mk loop ral;FatuEdlmanenAkg
$arr3 .
7. Joining Two Arrays with array_merge()
]TahrN_ 29
array_merge.php
>
<html>
<head>
<title>Joining Two array with array_merge()</title>
</head>
<body>
<h2>Using array_merge()</h2>
<?php
$arr1 = array("a", "b", "c");
$arr2 = array(1,2,3);
$arr3 = array_merge( $arr1,$arr2 );
foreach ( $arr3 as $val )
{
print "$val<br />";
}
?>
</body>
</html>
$arr3
statement
array
nwgbgajnUvbnMnFatu
.
array
pk nUvral;FatuEdlmanenAkg
$arr1
nig
$arr2
array
foreach
>
array_push()TTYlyknUv array nig tMlepSgBI parameters EdltMlTaMgenaHKWCaFatuEdlnwgRtUvbEnmeTAeGay
array . array_push() function
minmanlkNdUc array_merge()enaHeT BIeRBaH array
EdlbBan eTACa argument dMbgU nwgRtUvERbRbYl ehIy array_push() nwg pl;mkvijnUvtMlsrubTaMgGs
;EdlmanenAkg array enaHvij .
8. Adding Multiple Variables to an Array
Page 33
]TahrN_ 30
01:
02:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
<html>
<head><title>Adding Muliple variables to an Array</title></head>
<body>
<?php
$arr1 = array ("a", "b", "c");
$total = array_push( $arr1,1,2,3);
print "There are $total elements in \$arr1 ";
foreach ( $arr1 as $val )
{
print "$val<br/>";
}
?>
</body>
</html>
Page 34
<html>
<head>
<title>Remove first array element with array_shift()</title>
</head>
<body>
<h2>Using array_shift()</h2>
<?php
$user=array("M","U","T","H");
$total=count($user);
echo "Before use array_shift() threre are $total element<br/>";
foreach($user as $val)
{
echo "$val <br/>";
}
array_shift($user);
$total=count($user);
echo "after use array_shift() there are $total element</br>";
foreach($user as $val)
{
print "$val <br/>";
}
?>>
Page 35
</body>
</html>
>
array_slice() GnuBaateGayGkTajykcMnYnnFatu array EdlTamTareGayGkpln
; vU
argument cMnYnbI TImYyKWCa array variable
TI2CaTItaMEdlRtUvcab;epImyk nigTIbI KWCacMnnY Fatu rbs; array
EdlRtUvkarehIy argument TIbIenHKW (optional) RbsinebIGkmineRbInvU argument enHeT array_slice()
nigcab;yknUvFatuTaMgGs;n array cab;BITItaMgEdlGk)anpl;CatMlenA kg argument TI1mk.
]TahrN_ 33
10. Slicing Arrays with array_slice()
<html>
<head><title>Using array_slice()</title>
</head>
<body>
<h2>Using array_slice()</h2>
<?php
$first = array ("a", "b", "c", "d", "e", "f");
$second = array_slice($first, 2, 3);
foreach ( $second as $val )
{
print "$val<br />";
}
?>
</body>
</html>
Page 36
sUmBinitkg ]TahrN_ 34
<html>
<head><title> sorting array with sort() function</title>
</head>
<body>
<h2>Using sort() function </h2>
<?php
$s_array = array ("Bayon", "Taprom", "Angkor", "Presh vihear");
sort( $s_array );
foreach ( $s_array as $var )
{
print "$var<br />";
}
?>
</body>
</html>
Page 37
Page 38
Page 39
[[
Object
1. What Is an Object ?
KWCa bNM n variables nig functions EdlzitenAkg template BiessmYyEdl eKehAfa class .
object lak;nvU tMeNIrkarEdlmanenAxagkg
BIkareRbIR)as;va edayplC; a Interface sMrab;eGayGkbBan nUv
bBaaredIm,ITTYlBtmanRtlb;mkvij . interface TaMgenaHKWCa Function BiessEdleKeGayeQaH methods ehIyral;
methods TaMgGs;n object KWRtUvEt Access eTAkan; special variable ehAfa properties.
KuNsm,tri bs; object-oriented code KWpl;nUvkarkat;bnykarsresrkUd dEdlBIeRBaH class Edl)an
begtI Ca object manPaBgayRsYlykeTAeRbIR)as;BI project mYyeTAkan;Project mYyepSgeTot .
bEnmBIenHvamanlTPaBbegIt nUv child classes Edl inherit ehIynwgOverride nUvlkNepSgBI parents class .
Object
2. Creating an Object
>
>
>
>
GkGac test nUv $obj1 nig $obj2 CamYynwg gettype() function . gettype
nimYyehIypl;Ca string mkvijEdlR)ab;GkGMBIGEVI dlGkkMBgu EteFVIkarCamYy .
function
TTYltMlVariable
3. Object Properties
{
var $name = "Vichet";
}
>
>
Page 40
eyIg)an declared nUv variable CamYynwg var keyword EdlenHKWCaviFIsaRsEtmYyKt;edIm,I declare nUv property
mYyenAkgPasa PHP 4 eyIgnwgeXIjBIlkNbEnmrbs; PHP 5 enA]TahrN_eRkayeTot .
dUecHRbsinebIGkcg;sresrkUdeGay compatible CamYy PHP 4 enaH GkRtUvEteRbI var keyword.
\LvenH Person objet Edl)anbegtI KWmanpk nUv property mYy EdleGayeQaHfa $name
CamYynwgtMlrbs;vaKW
"Vichet" . GkGac access nUv
property enH BIxageRkA object
nigeFVIkarpas;br tMlrbs;vaedayeRbI]TahrN_ 37 .
<?
class Person >
{
var $name = "Vichet"; >
}
$obj1 = new Person(); >
$obj2 = new Person(); >
$obj1->name = "Soporn"; >
print "$obj1->name<br />"; >
print "$obj2->name<br />"; >
?>
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
<?php
class Person
{
var $name = "Vichet";
function getName()
{
return "Ratana";
}
}
$person = new Person();
print $person->getName();
// outputs "Ratana"
?>
Page 41
>
]TahrN_ 39
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
<html>
<head><titile>Accessing a property from within a method</title>
</head>
<body>
<h2>Accessing a property from within a method</h2>
<?php
class Person
{
var $name = "Thary";
function getName()
{
return $this->name;
}
}
$person = new Person();
$person->name = "Bopha";
print $person->getName();
//outputs "Bopha"
?>
</body>
</html>
Page 42
>
]TahrN_ 40
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
<html>
<body>
<head><title>Changing the value of a property from within a method</tile>
</head>
<body>
<h2>Changing the value of a property from within a method</h2>
<?php
class Person
{
var $name = "tepy";
function setName( $n )
{
$this->name = $n;
}
function getName()
{
return $this->name;
}
}
$person = new Person();
$person->setName("darya");
print $person->getName();
// outputs "darya"
?>
</body>
</html>
Page 43
<html>
<head>
<title>A Class with a Construct</title>
</head>
<body>
<h2>A Class with a Construct</h2>
<?php
class Person
{
var $name;
function Person($name="tepy")
{
$this->name = $name;
}
function setName( $n)
{
$this->name = $n;
}
function getName()
{
return $this->name;
}
}
$person = new Person("darya");
print $person->getName ();
// outputs "Darya"
?>
</body>
</html>
Page 44
>
>
min)anpl;nUvkar karBarsMrab; object properties enaHeT Client code Gac getb set object
properties)aneTAtamkarKitrbs;BYkeK.mansMnYrsYrfaetImanbBaaGVeI TkgkarGnuvtn_EbbenH? vaminmanCabBaakg
kareRbI
R)as;nvU Public accessible properties enaHeT Edl CaTUeTA vaCakarGnuvtns_ Mrab;kar access eTAkan; object
EdlmanlkNtUc. enA]TahrN_xag eRkameyIgnwg)aneXIjnUvlkN
mYyEdlkMNt;nUvRBMEdnnkar access eTAkan;
$name propertyrbs; Person class .
]TahrN_ 42 Class with Public Properties
PHP 4
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
<?php
class Person
{
var $name;
var $pid;
var $personStr;
function Person( $name="somphy", $pid=0 )
{
$this->name = $name;
$this->pid = $pid;
}
function setName( $n )
{
$this->name = $n;
$this->$personStr=$this->name." ".$this->pid;
}
function getName ()
{
return $this->name;
}
}
$person = new Person("sovan",5233);
print $person->PersonStr();
// outputs "sovan 5233"
Page 45
28:
29:
30:
Privacy Level
Description
public
private
protected
dUecHeyIgGacpas;br
keyword vij .
properties
var
ex.
private $name;
private $pid;
>
>
\LvenHkarBayampas;br nUvtMlrbs;
message dUcxageRkam .
Fatal
error:
Cannot
access
classPrivateProperty.php on line 30
$name property
private
property
in
c:\Inetpub\wwwroot\
>
dUecH Client coders RtUvEteRbIR)as;nvU setName() method edIm,IeFVkI arEkERbeTAelItMlrbs; $name property .
maneBlNamYyGkRbEhlCacg;eGay child classes Gac accsess eTAkan; property
EdlenAkg eBlenaHGkKYrEteRbInUv protected keyword eRBaHvaGnuBaateGayGk accesse vaBClass EdlGk)an
derived ehIyeyIgnwg)aneXIjvaenAkg
EpkmYyEdlniyayBI Inheritance .
9. Limiting Access to Object Methods >
lkNsMxan;n object-oriented code KWCa class . Object RtUvEtkMNt;nUvmuxgarnig Public interface
eGay)anc,as;las;enAeBlGkbegItnUv methods epSg .bMENkEdlmanenAkg classKWpknUvmuxgarepSgsMrab;tM
eNIrkardUecHGkKYrEtlak;nUvmuxgarTaMgenaHBIBiPBxageRkA. sMrab;Ca]TahrN_xagelIeyIgKYrEtbegtI nUv method
sMrab; $personStr property BIeRBaHral;tMeNIrkarTaMgGs;rbs; $personStr RtUv)anpkenAkg setName() method .
function setName( $n )
{
Developing web Application with PHP
Page 46
$this->name = $n;
$this->$personStr=$this->name." ".$this->pid;
>
GkRbEhlCaRtUvkarnUv method edIm,I reset nUv string rbs; $personStr dUecHeyIgnwgbegtI nUv
Method fm
I YysMrab; pl;tMleTAeGay $personStr property .
}
function setName( $n )
{
$this->name = $n;
$this->makePersonStr( $n, $this->code );
}
function makePersonStr( $string , $code)
{
return $this->personStr = "$string $code";
}
\LvenH makePersonStr() function Gac access )anEtenAkg method TaMgLayNaEdlzitkg Person class
b:ueNaH .
Public, protected ehIynwg private vatMeNIr)anEtCamYynwg PHP 5 Etb:eu NaH dUecHRbsinebIGkBayam
eRbInUv keyword TaMgbIenHCamYy PHP 4 enaH script rbs;GknwgRtUvbraCyTaMgRsug .
10.Inheritance
>
edIm,IbegtI class mYyEdlGac inherits function BI parent class eyIgRbEhlCaRtUvEkERbnUvkar declare class
rbs;eyIgbnicbnc . ]TahrN_fIxageRkamenH KWCakarbegItnUv Item Class ehIynig begtI nUv inheriting class
Edlple; QaHfa PriceItem .
]TahrN_ 43
Creating a Class That Inherits from Another
01:
02:
03:
04:
05:
06:
07:
>
<html>
<body>
<h1>Creating Class That Inherits from Another</h1>
<?php
class Item
{
Page 47
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
}
$item = new PriceItem( "Angkor", 4545 );
print $item->getName ();
// outputs "Angkor"
?>
</body>
</html>
var $name;
function Item( $name="item", $code=0)
{
$this->name = $name;
$this->code = $code;
}
function getName()
{
return $this->name;
}
<?php
Page 48
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
class Item
{
var $name;
function Item( $name="item", $code=0)
{
$this->name = $name;
$this->code = $code;
}
function getName()
{
return $this->name;
}
}
class PriceItem extends Item
{
function getName()
{
return "(price)."$this->name;
}
}
$item = new PriceItem( "widget", 5442 );
print $item->getName();
// outputs "(price) Angkor"
?>
class Item
{
private $name;
// ...
}
Page 49
]TahrN_ 45
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
<?php
class Item {
private $name;
function __construct( $name="item", $code=0 ) {
$this->name = $name;
$this->code = $code;
}
function getName() {
return $this->name;
}
}
class PriceItem extends Item {
function getName() {
return "(price) ".parent::getName ();
}
}
$item = new PriceItem ("widget", 5442);
print $item->getName();
// outputs "(price) widget"
?>
<?php
class Item
{
private $name;
function __construct( $name="item", $code=0 ) {
$this->name = $name;
$this->code = $code;
}
function getName () {
return $this->name;
}
Page 50
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
}
class PriceItem extends Item
{
private $price;
function __construct( $name, $code, $price ) {
parent::__construct( $name, $code );
$this->price = $price;
}
function getName() {
return "(price) ".parent::getName ();
}
}
$item = new PriceItem ("widget", 5442, 5.20);
print $item->getName ();
// outputs "(price) widget"
?>
Page 51
enAelI
Form
<html>
<head>
<title>Registration form </title>
</head>
<body>
<CENTER>
<h2>Registration Form</h2>
<hr width="50%">
<form method="GET" action="register.php" >
<table>
<tr><td>First-Name</td>
<td><input type="Text" name="txtFname"></td>
</tr>
<tr><td>Last-Name</td>
<td><input type="Text" name="txtLname"></td>
</tr>
<tr><td>Date of Birth</td>
<td>
<Select name="cboday">
<option value="01">01</Option>
<option value="02">02</Option>
<option value="03">03</Option>
</select>
<Select name="cbomonth">
<option value="01">Jan</Option>
<option value="02">Feb</Option>
>
<option value="03">Mar</Option>
</select>
<Select name="cboyear">
<option value="1980">1980</Option>
<option value="1981">1981</Option>
<option value="1982">1982</Option>
<option value="1983">1983</Option>
</select>
</tr>
Developing web Application with PHP
Page 52
<tr><td>E-mail</td>
<td> <input type="text" name="txtemail"></td>
</tr>
<tr><td align=right colspan=2> <input type="submit" value="Submit">
</td>
</tr>
<tr><td>Password</td>
<td> <input type="password" name="txtpwd" size=22></td>
</tr>
</table>
</body> </html>
$_GET['txtFname'];
$_GET['txtLname'];
$_GET['cboDay'];
$_GET['cboMonth'];
$_GET['cboYear'];
$day."-$month"."-$year";
print "<table>";
print "<tr>";
print "<td><b>First-Name :</b></td>";
print "<td> $fname </td>";
Developing web Application with PHP
Page 53
print "</tr>";
print "<tr>";
print "<td><b>Last-Name :</b></td>";
print "<td> $lame </td>";
print "</tr>";
print "</table>";
?>
</body>
</html>
get mehod
. sUmBinitemIlnUv
address
Page 54
?>
</body>
</html>
Gknwg)aneXIjBIPaBxusKarvagkareRbIR)as;nUv post method nig get method enAelI Address bar rbs;
brower nimy
Y . xageRkamKWCalTplEdlTTYl)anbnab; BI form RtUv)an Submit edayeRbIR)as;nUv post method .
>
]TahrN_ 49 pl;lTPaBeGayGkTTYlnUvBtmanBI HTML element Edl)an Submit
nUvtMlmYysMrab;Etelement mYyb:ueNaHEdlenHBMuTan;manlkNRKb;RKan;enAeLIyenaHeTRbsin ebIGkcg;eFVIkarCamYynwg
multiple select .
]TahrN_ 50
2. Accessing Form Input with User-Defined Arrays
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
<html>
<head>
<title>HTML Form with a 'select' Element</title>
</head>
<body>
<center>
<h2>Multiple Select</h2>
<hr width="50%">
<form action="multiple.php" method="POST">
<select name="products[]" multiple="multiple">
<option> Cocacola </option>
<option> Fanta </option>
<option> Sprite </option>
<option> Merinda </option>
<option> Pepsi </option>
</select><br><br>
<input type="submit" value="submit">
</body>
</html>
eyIgnwgbegIt script edIm,IEsVgrk input BI "products[]" form element EdlbegtI enAbnat;TI 12edaypl;eQaHCa array
indexed ehAfa
products. products[]KWCa select element Edlple; GaynUvCMerIssMrab;GkeRbIR)as;edaykareRbI
option element enAbnat;TI 13 dl; bnat;TI 17eyIgnwgbgajBIlTplEdlGkeRbIR)as;)aneRCIserIsBI form xagelI
EdlbegtI Ca array enAkg ]TahrN_ 51
01:
02:
03:
<html>
<body>
<head>
Page 55
04:
05:
06:
07:
08:
09:
10:
08:
10:
11:
12:
13:
14:
15:
16
17:
18:
19:
Page 56
multiple.php
]TahrN_ 52
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
08:
10:
11:
12:
13:
14:
15:
16
17:
18:
19:
<html>
<body>
<head>
<title>Reading Input from the form multiple select</title>
</head>
<?php
if ( is_array( $_POST['products'] ) )
{
echo "<b>Your products choice are :</b><br/>";
foreach($_POST['products'] as $val)
{
print "$val<br>";
}
}
?>
</body>
</html>
Page 57
MySql
>
MySql KWCaRbePT Open Source Sql databases management system EdlmanRbCaRbiybMpt
u sMrab;
kareRbIR)as; edayRtUv)anGPivDn_ngi KaMRTeday MySQL AB . MySQL AB KWCaRkumhnu EdlbegtI eLIg
edayRkumGkGPivDn_ MySql .
1.1 MySQL is a relational database management system. >
A relation databases KWrkSaTuknUvTinny
kg table epSgBIKaEdlmanlkNRbesIrCagkarrkSaTuknUvTinnyTaMg
Gs;enAkg storeroom dFmM Yy ehIyviFIenHGacCYybegnI el,n nig manPaBgayRsYleRbIkan;EtRbesIreLIgCamYy
nwgTinny . SQL KWCabMENkmYyn MySQL Edl eRbI CaBakeBjfa Structured Query Language SQL
KWCaPasaEdleRbIR)as;Ca standard edIm,I access eTAkan; databases ehIyRtUv)ankMNt;eday ANSI/ISO SQL
Standard .SQL standard
RtUv)anvivtn_eLIgenAkgGMLgqaM 1986 EdlbegtI )anCaCMnan;rbs; SQL-92
edaysMedAeTAelI SQL standard EdleFVkI ardak;ecjnUvCMnan;rbs;xnenAkg GMLugqaM 1992 nig bnab;mkKW SQL-1999
nigeRkaymkeTotKW SQL-2003 .
1.2 MySQL software is Open Source. >
Open Source KWmannyfaGkeRbIR)as;manlTPaBEkERbnUv software enaH)an ehIy GacTajyknUv
Software TaMgenaHenAelI Internet nigGaceRbIR)as;va edayminmankarbg;R)ak; eTAelIesvakmGT
VI aMgGs; . MySQL
software eRbIR)as; GPL( General Public License ) edIm,Ipl;eGayGknUvGVIEdlRtUvkar nig GVIEdlGkminRtUvkarcMeBaH
software enH . KWmannyfa RbsinebIGkminmanGarmN_Tukcit CamYynig
w GPL b Gkcg;bgbn; UvkUdeTAkg MySQL
edIm,IeFVCI aCommercial application GkGacTijnUvc,ab;GnuBaatieGayeFVBI aniCkmBIRkumhun MySQL Edl
BtmanlMGti GkGaceRbIR)as;nvU eKhTMBr (http://www.mysql.com/company/legal/licensing/)
1. What is MySql ?
>
EpkxageRkamnwgbgajBIkareRbIR)as; SQL commands enAkg MySQL context .
RKb; commands TaMgGs;suTEt)anTTYlsal;eday MySQL system kgGMLg eBlEdl design SQL databases
environment . command rbs; MySQL nimy
Y RtUvEtbBab;CamYynwgsBaa Semicolon ( ; ) .
show databases KWCa command dMbg
U EdleRbIedIm,IbgajnUveQaH database TaMgGs;EdlmanenAkg MySQL
kg enaHman database cMnnY BIrPab;mkRsab;CamYy MySQL package.
2. SQL commands in mysql
Page 58
+----------+
2 rows in set (0.00 sec)
users
epSg .sMrab;Ca]TahrN_
>
eyIgGacBinitemIl database Edl)anbegtI xagelIedayeRbIR)as; show database command.
>mysql> show databases;
syntax : mysql>create database BbuDb ;
+------------+
| Database |
+------------+
| mysql
|
| BbuDb |
| test
|
+------------+
3 rows in set (0.00 sec)
>
ral; attribute nimYysuTEtRtUv)ankMNt;nvU cMMnYntYGkSrEdlRtUvrkSaTukenAkg table ehIy fieldnimYyn table
suTE tTamTareGaymannUvtMlCanic . cMENk id attribute tMlrbs;vaniwgRtUvekIneLIgedaysVy RbvtienAeBl
EdlTinnyRtUv)anbBaleTAkan; tblpeople mYyelIk .varchar KWCatMlEdleRbICatYGkSr bCatYelx cMENk
binary eKeRbIedIm,IeFVIeGaytMl eTACa case-sensitive rI name ehIy nig email attribute KWminRtUv)aneRbI binary
enaHeT dUecHvaminmanlkN case-sensitive eLIy . id KWRtUv)anbegItCa primary key sMrab; table edIm,I
identify row nimYyn table .
mysql> desc tblpeople ;>
);
Page 59
>
+----------+-------------+------+-----+---------+--------------+
| Field
| Type
| Null | Key | Default | Extra
|
+----------+-------------+------+-----+---------+--------------+
| id
| int(5)
|
| PRI | NULL
|auto_increment
| username | varchar(20) |
|
|
|
| password | varchar(20) |
|
|
|
| name
| varchar(20) |
|
|
|
| email
| varchar(30) |
|
|
|
+----------+-------------+------+-----+---------+--------------+
5 rows in set (0.01 sec)
>
mysql>insert into tblpeople values(null, 'lou', ' army ', ' lou cy ', ' lou_cy@gmail.com ');
>
enAeBlEdl id attribute Ca auto_increment eyIgeRbI null sMrab;Ca data entry Edl null Value
minmanlkNdUcnwg blank (" ") enaHeT . id attribute cab;epmI edaytMlCa integer 1 ehIyvanwgeFVIkarbegIntMl 1
nenAeBlEdlmankarbBal Tinny eTAkan; row fmI g. GkGacBinitemIlBtman TaMgGs;EdlmanenAkg
tablebnab;BI)anbBaledayeRbIR)as; SQL command mysql>select * from tblpeople ; >
+----+----------+----------+------------+---------------------+
| id | username | password | name
| email
|
+----+----------+----------+------------+---------------------+
| 1 | muth
| muth07
| monen
|muth_monen@hotmail.com |
| 2 | lou
| army
| lou cy
|lou_cy@gmail.com
|
+----+----------+----------+------------+---------------------+
2 rows in set (0.00 sec)
edIm,Ilb;nvU table NamYyecjBI database GkGaceRbI command :mysql> drop table tableNamesMrab; command
EdleRbIedIm,Ilb; row BI table TamTareGayGkpln; vU eQaHn table ehIynigwhere condition EdlbBaak;BI row(s)
EdlnwgRtUvlb; .
delete from tableName where Where-condition
sUmRbugRbytRbsinebIGkmineRbI
where-condition
enaHeT RKb; rows TaMgGs;EdlmanenAkg table
nwgRtUvlb;ecalTaMgGs;. enAkg ]TahrN_xageRkameyIgnwgeRbItM lrbs; attribute name nig username edIm,Ilb; row
ecjBI table tblpeople .
delete from tblpeople where name= monen and username = muth ;>
UPDATE command RtUv)aneRbIedIm,IEkERbBtmanEdlmanenAkg table GkGac select yk
BtmanNamYymkEkERbBtmanedayminmankar)at;bg;nvU record b row edImTaMgRsugeLIy .
The syntax is:
UPDATE table-name SET field1=val1, field2=val2, field3=val3
WHERE condition;
ex.
update tblpeople set password=maco where username=muth ;
Page 60
3. PHP/MySQL Functions
>
>
muneBlEdlcab;epmI eFVIkarCamYynwg database rbs;Gk GkRtUvEt connect eTAkan; Server Camunsin .
PHP pl;nUv mysql_connect () function edIm,IGnuvtn_nUvParkicenH .
bMENkkUdxageRkamnwgbgajBIkareRbIR)as; mysql_connect() Pab;eTAkan ; MySQL database Server .
3.1 Connecting to MySQL
>
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
<html>
<head>
<title>Listing 13.2 Adding a Row to a Database</title>
</head>
<body>
<div>
<?php
$user = "root";
$pass = " ";
$db = "BbuDb";
$link = @mysql_connect( "localhost", $user, $pass );
Page 61
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
if ( ! $link )
{
die( "Couldn't connect to MySQL: ".mysql_error() );
}
print "<h2>Successfully connected to server</h2>\n\n";
@mysql_select_db( $db )
or die ( "Couldn't open $db: ".mysql_error() );
print "Successfully selected database \"$db\"<br />\n";
$query = "insert into tblpeople(username , password , name , email )
values( ' ratana07 ', ' 1234 ', ' som ratana ', ' ratana@example.com ' )";
mysql_query( $query, $link )
or die ( "INSERT error: ".mysql_error() );
mysql_close( $link );
?>
</div>
</body>
</html>
Page 62
<tr>
<td>Email :</td>
<td><input type="TextBox" size="18" name="txtemail"/></td>
</tr>
<tr>
<td align="right" colspan="2">
<input type="Submit" value="Submit">
</td>
</tr>
</table>
</body>
</html>
Inser.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>
<html>
<head>
<title>Listing 13.3 Adding user input to a database</title>
</head>
<body>
<?php
$username = $_REQUEST['txtusr'] ;
$name =$_REQUEST['txtname'] ;
$pwd =$_REQUEST['txtpwd'] ;
$email = $ REQUEST['txtemail'] ;
if ( ! empty($username) && ! empty( $name ) && ! empty($pwd )&&
!empty($emial) ) // check user input here!
{
$db = "BbuDb";
Page 63
eyIg)an select nUv database Edlpk table eQaHfa tblperson enAbnat;TI 22 ehIynwgbegIt SQL query
edIm,IbBaltMlEdl)anmkBI user-submitted edaybBanBtmaneTAeGaymysql_query() enAbnat;TI 26 .
3.5 Accessing a Resultset >
GkGacmanPaBgayRsYledIm,ITTYl)an array BICYr nimYyn filds edaykareRbIR)as; CamYynwg mysql_fetch_row()
function . function enHtMrUveGayGkpl; result resource mYy ehIy vanwg return vijnUv row nimYyEdlmanenAkg
field .
Listing All Rows and Fields in a Table
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23
24:
25:
26:
27:
28:
29:
30: }
31:
32:
33:
34:
35:
<html>
<head>
<title> Selecting Data</title>
</head>
<body>
<?php
$user = "root";
$pass = "1235";
$db = "BbuDb";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
{
die( "Couldn't connect to MySQL: ".mysql_error() );
}
mysql_select_db( $db, $link )
or die ( "Couldn't open $db: ".mysql_error() );
$result = mysql_query( " SELECT * FROM tblperson " );
$num_rows = mysql_num_rows( $result );
print "<table border=\"1\">\n";
while ( $a_row = mysql_fetch_row( $result ) )
{
print "<tr>\n";
foreach ( $a_row as $field )
{
print "\t<td>".$field."</td>\n";
}
print "</tr>\n";
print "</table>\n";
mysql_close( $link );
?>
</body>
</html>
bnab;BIeyIg)an connected eTAkan; database server ehIynwg selected database rYcral;eyIg )aneRbI mysql_query()
enAbnat;TI 18 edIm,IbBan select statement eTAkan; database serverbnab;mkeyIgrkSarnUv return result resource
enAkg varible mYyeQaHfa $result ehIyeRbIvaedIm,I TTYlykcMnYn row EdlmanenAkg table .
sMrab; test expression n while statement enAbnat;TI 22 eyIg)anpl;nUvlTplEdl)anBI
mysql_fetch_row() eTAeGay variable $a_row. zitkgtMeNIrkarrbs; while statement
Developing web Application with PHP
Page 64
eyIg)an loop nUv row array Edlpk kg $a_row enAbnat;TI 25 ehIyeFVkI arbecjnUvlTplBIFatunmi Yy
bBal eTAkg table cell rYcRtUv)anbgajeday web browser . GkkGac accessFields
edayeRbIR)as;eQaHrbs;vaeTAtamviFsI aRsBrI ya:gKW mysql_fetch_row() va return Ca
Numerics
array
nig mysql_fetch_row() va return Ca associative array
CamYynwgeQaHrbs;Fields edayeRbICa keys rbs; array. bMENkkUdxageRkameyIgnwgCMnYsbnat;TI 23-33
edaykareRbIR)as; mysql_fetch_array() CMnsY vij .
print "<table border=\"1\">\n";
while ( $a_row = mysql_fetch_array( $result ) )
{
print "<tr>\n";
print "<td>".$a_row['name']."</td>";
print "<td>".$a_row['username']."</td>";
print "<td>".$a_row['email']."</td>";
print "</tr>\n";
}
print "</table>\n";
object
eday eRbI
>
print "<td>".$a_row->name."</td>";
print "<td>".$a_row->username."</td>";
print "<td>".$a_row->email."</td>";
print "</tr>\n";
}
print "</table>\n";
Page 65
Page 66
>
>
1. Cookie
Auth.php
<?php
class Auth
{
function Auth()
{
mysql_connect('localhost', 'root');
mysql_select_db('my_own_bookshop');
}
public function authUser($user , $password)
{
$q = ' SELECT username , password FROM tblpeople WHERE
password ="'.$password. '"' ;
$r = mysql_query($q);
if (mysql_num_rows($r) == 1)
{ return TRUE; }
else { return FALSE; }
}
}?>
login.php
>
<?
ob_start();
?>
<html>
<head><title>Login</title></head>
<body>
<?php
require("auth.php");
$auth = new Auth();
Page 67
Page 68
KWCakalbriecTEdlknghYseTAmYyf nwgCakalbriecTnGtItkal
edIm,IR)ab;eGay browser bM)at;ecalnUv cookie data enaH .
2. SESSIONS >
PHP
session
GnuBaatieGay application rbs;GkrkSarTukBtmanenAkgcurrent session
xNeBlEdlGkeRbIR)as;)an logged in eTAkan; application rbs;Gk . session RtUv)ankMNt; eday session ID
EtmYyKt; PHP begtI session ID eday MD5 hash n remote IP addressnig bEnmnUvsBaaepSgeday random
eTAkg hexadecimal string . eyIgnwgsresreLIgvijnUv]TahrN_elIkmun EdleyIg)aneRbIR)as; cookie
mkCMnsY edaykareRbIR)as; session vij .
time()-86400
login.php
<html>
<body>
<?php
require("auth.php");
$auth = new Auth();
session_start();
if (isset ($_POST['login']) && ($_POST['login'] == 'Log in') &&
$auth->authUser($_POST['txtuid'] , $_POST['txtpwd'] ) )
{
$uid = $_POST['txtuid'];
$_SESSION['uid'] = $uid;
header("Location: http://localhost/index.php");
exit();
}
else
{
?>
/* HTML form comes here */
Developing web Application with PHP
Page 69
<?php
}
?>
</body>
</html>
index.php
>
<?php
if (isset ($_SESSION['uid']) && $_SESSION['uid'])
{
?>
<html>
<head><title>Index page</title></head>
<body>
Logged in with UID: <?php echo $_SESSION['uid']; ?><br />
<a href='logout.php'>Log out</a>.
</body>
</html>
<?php
}
else
{
/* If no UID, we redirect to the login page */
header('Location: http://localhost/login.php');
}
?>
logout.php
<?php
session_start();
$_SESSION = array();
session_destroy();
header('Location: http://localhost/login.php');
?>
eyIgenAEt
Page 70