COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.

COM
Mon hoc: PHP

Bu¡ 1

Nhöõng vaán ñeà chính seõ ñöôïc ñeà caäp trong baøi hoïc:
9 Giôùi thieäu PHP
9 Caáu hình IIS, Apache Web Server
9 Caøi ñaët PHP.
o Caøi ñaët PHP.
o Caáu hình öùng duïng PHP
9 Giôùi thieäu PHP.
o PHP Script.
o Ghi chuù trong PHP
o In noäi dung baèng PHP
1. G¡O¡ TH¡K¡ PHP
PHP vIof faf cua chu PorsonaI Homo Pago ra doI nam 1994 do phaf mInh cua
Rasmus Iordorf. va no fIop fuc duoc phaf frIon boI nhIou ca nhan va fap fho khac. do do
PHP duoc xom nhu mof san pham cua ma nguon mo.
PHP Ia kIch ban frình chu |sorvor scrIpf) chay fron phía sorvor |sorvor sIdo) nhu
cach sorvor scrIpf khac |asp. jsp. coId fusIon).
PHP Ia kIch ban cho phop chung fa xay dung ung dung vob fron mang Infornof hay
Infranof fuong fac voI moI co so du IIou nhu mySQI. PosfgroSQI. OracIo. SQI Sorvor va
Accoss.
Iuu y rang. fu phIon ban 4.0 fro vo sau moI ho fro sossIon. ngoaI ra PHP cung nhu
PorI Ia kIch bang xu Iy chuoI raf manh chính vì vay ban co fho su dung PHP frong nhung
co you cau vo xu Iy chuoI.
2. CA¡ OAT PHP
CaI daf PHP fron non WIndovs fhì su dung php-4.0.6-WIn32.zIp. sau khI caI daf
ung dung nay fron dIa cung so xuaf hIon fhu muc PHP. frong fhu muc nay so co fap fIn
php4fs.dII va php.oxo cung voI fhu muc sossIondafa.
NgoaI ra. frong fhu muc WINÐOW hoac WINNT so xuaf hIon fap fIn php.InI. fap
fIn nay cho phop ban cau hình cho ung dung PHP. Chang han. khI su dung sossIon. PHP
can mof noI do Iuu fru chung. frong fap fIn nay mac dInh Ia sossIon.savo_pafh =
C:`PHP`sossIondafa. nou ban caI daf PHP voI fhu muc PHP fron dIa Ð fhì ban can fhay
doI duong dan frong khaI bao nay.
Tuong fu nhu vay. khI co IoI frong frangPHP fhì IoI fhuong xuaf hIon khI frIou goI
chung. do cho dau cac IoI nay fhì ban can khaI bao dIspIay_orrors = Off fhay vì chung o
frang fhaI dIspIay_orrors = On.
NgoaI ra. frang PHP cung co fho frình bay mof so varnIng khI chung phaf hIon cu
phap khong hop Iy. chính vì vay do cho dau cac varnIng nay fhì ban cung can khaI bao
frang fhaI Off fhay vì On nhu assorf.varnIng = Off.
3. CA¡ H¡NH ¡NG O¡NG PHP
3.1. Cuu hInh ¡¡S
Sau khI caI daf ho dIou hanh WIndovs NT hay 2000 fro vo sau. bang cach khaI bao
moI mof vob sIfo hay vIrfuaI sIfo frong mof sIfo dang co fhoo cac buoc nhu sau:
1. Tao mof fhu muc co fon myPHP do Iuu fru cac fap fIn PHP
2. KhoI dong IIS |fu dong khoI dong nou WIndovs NT/2000)

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
3. Chon Sfarf 1 Programs 1 AdmInIsfrafIvo TooIs 1 Infornof InformafIon Sorvor
4. Nou fao vIrfuaI sIfo fhì chon ÐofauIf Wob Sfo 1 R-CIIck 1 Nov 1 VIrfuaI SIfo
5. Trong fruong hop fao moI SIfo fhì ÐofauIf Wob Sfo 1 R-CIIck 1 Nov 1 SIfo
6. Nou chon fruong hop 4 fhì ban cung fap dIon gIaI cua sIfo nhu hình 1-1




Hình 1-1: KhaI bao dIon gIaI
7. Chon nuf Noxf va khaI bao IP va porf. frong fruong hop ban khong su dung porf
80 cho ung sIfo khac fhì chon gIa frI mac dInh. Tuy nhIon nou co nhIou ung
dung fruoc do da cau hình frong IIS fhì ban co fho fhay doI porf khac. ví du
chon porf 85 nhu hình 1-2.




Hình 1-2: KhaI bao IP va Porf
Iuu y rang. porf 80 Ia porf chuan dIou nay co nghIa Ia khI frIou goI fron frình
duyof ban khong can go porf. ví du hffp://IocaIhosf/. ÐoI voI fruong hop porf khac fhì ban
phaI go fuong fu nhu hffp://IocaIhosf:85/

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
8. Chon Noxf. ban chon fhu muc cua ung dung. doI voI fruong hop nay chung fa
chon vao fhu muc myPHP. chang han frong fruong hop nay chung fa chon hfu
muc myPHP nhu hình 1-3.



Hình 1-3: Chon fhu muc myPHP
9. Ko don chon quyon fruy cap vob sIfo. frong fruong hop dang fhIof ko fhì ban
chon vao Ðrovso. NgoaI ra. nou ban cho phop nguoI su dung Infornof co fho
fhuc fhI fap fIn fhuc fhI fu xa fhì chon vao fuy chon oxocufo.



Hình 1-4: Quyon fruy cap
10.Chon Noxf va IInIsh. frong cua so IIS xuaf hIon ung dung co fon myPHP |khaI
bao frong phan dIon gIaI) nhu hình 1-5.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM



Hình 1-5: Tao fhanh cong ung dung PHP frong IIS

11.Sau khI fao ung dung xong. ban chon fon ung dung myPHP 1 R-CIIck l
ProporfIos 1 cua so xuaf hIon nhu hình 1-5.




Hình 1-5: Cau hình PHP frong IIS
12.Ðang cach chon vao nuf ConfIgurafIon. cua so so xuaf hIon nhu hình 1-6.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM



Hình 1-6: Thom PHP ÐngIno

13.Chon nuf Add. va khaI bao nhu hình 1-7.



Hình 1-7: KhaI bao PHP ÐngIno
14.Ðo kIom fra ung dung. ban mo cua so IÐ va go fron fhanh dIa chI chuoI nhu sau:
hffp://IocaIhosf:85/ . kof qua xuaf hIon nhu hình 1-8.



CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 1-8: !ng dung PHP da duoc khoI dong
3.2.
4.
Cu¡ dut Apuche Web Servr
Ðo caI daf Apacho Wob Sorvor. ban fhoo cac buoc sau
1. Chop fap fIn apacho_1.3.22-vIn32-x86.oxo xuong dIa cung
2. Chay fap fIn nay va caI daf Ion dIa C:`Program IIIos`. sau khI kof fhuc fhanh
cong phan caI daf Apacho. ban baf dau cau hình ung dung PHP.
3. Chop ba dong Ionh fu fap fIn InsfaII.fxf frong fhu muc C:`PHP

:·rirtAìi¬: rDr `·:rDr`
AccTvr- ¬rrìi·¬ticD×÷Dttrc÷rDr .rDr
A·ticD ¬rrìi·¬ticD×÷Dttrc÷rDr `rDrrDr.-×-`

4. Pasfo vao fap fIn hffpd.conf frong fhu muc C:`Program IIIos`Apacho
Croup`Apacho`Conf`
5. Chon Sfarf 1 Programs 1 Apacho HTTP Sorvor 1 ConfroI Apacho Sorvor 1 Sfarf
6. VIof frang fosf.php voI noI dung <7ocho "hoIIo";7>
7. Chop fap fIn fosf.php vao fhu muc C:`Program IIIos`Apacho
Croup`Apacho`hfdocs`
8. Sau do go fron frình duyof hffp://IocaIhosf/fosf.php
G¡O¡ TH¡K¡ PHP
4.1. Yeu cuu
PHP dua fron cu phap cua ngon ngu Iap frình C. chính vì vay khI Iam vIoc voI PHP
ban phaI Ia nguoI co kIon fhuc vo ngon ngu C. C++. VIsuaI C. Nou ban xay dung ung
dung PHP co kof noI co so du IIou fhì kIon fhuc vo co so du IIou MySQI. SQI Sorvor hay
OracIo Ia dIou can fhIof.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
4.2.
4.3.
4.4.
G¡o¡ th¡eu
PHP Ia kIch ban frình chu |Sorvor ScrIpf) duoc chay fron non PHP ÐngIno. cung
voI ung dung Wob Sorvor do quan Iy chung. Wob Sorvor fhuong su dung Ia IIS. Apacho
Wob Sorvor. ...
Thong djch trung PHP
KhI nguoI su dung goI frang PHP. Web Server frIou goI PHP ÐngIno do fhong dIch
|fuong fu nhu ASP 3.0 chI fhong dIch chu khong phaI bIon dIch) dIch frang PHP va fra vo
kof qua cho nguoI su dung nhu hình 1-9.












Hình 1-9: Qua frình fhong dIch frang PHP

Tap fIn
PHP
Wob
Sorvor
PHP
ÐngIno
P
Parso
Rosponso
Roquosf
Parso
Roquosf
Rosponso
Kjch bun (scr¡pt)
NoI dung cua PHP co fho khaI bao Ian Ion voI HTMI. chính vì vay ban su dung cap
dau gIa <7=frI/bIou fhuc/bIon7> do khaI bao ma PHP. Chang han. chung fa khaI bao:

<rr>
ì÷Ci¬ tri ri-D :tr: <.:orcoric.>
2÷Ci¬ tri ri-D i: <.:i.>
·÷Ci¬ tri ·o tD-: <.ìD.>

Chang han ban khaI bao frang hoIIo.php voI noI dung nhu ví du 1-1 sau:
VI du 1-1: Trung hello.php

<IT\L>
<IFAL>
<TíTLF>::'-ì·c.- tc JIJ<TíTLF>
<IFAL>
<FOLY>
Cr--tiDo: <.`I-ììc JIJ`.>
<FOLY>
<IT\L>

Kof qua fra vo nhu hình 1-10 khI frIou goI frang nay fron frình duyof.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM



Hình 1-10: Kof qua frang hoIIo.php
Trong fruong hop co nhIou khaI bao. ban su dung ScrIpfIof. dou nay co nghIa Ia su
dung cap dau fron nhu <7php KhaI bao 7> voI cac khaI bao PHP voI cu phap cua C nhu
sau:

<.rDr
::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo``;
.>
÷KD¬i r¬c tr-D ì¬ :·rirtì-t
Ci¬ tri ·o¬ r¬oiDo: <rr>
<. :r¬oiDo .>
÷KD¬i r¬c D¬v ì¬ :·rirt

Iuu y rang. kof fhuc moI cau Ionh phaI dung dau ;
Ví du. ban khaI bao doan PHP fron frong fap fIn scrIpf.php nhu ví du 1-2
VI du 1-2: Trung scr¡pt.php

<IT\L>
<IFAL>
<TíTLF>::'-ì·c.- tc JIJ<TíTLF>
<IFAL>
<FOLY>
<.rDr
::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo`Cc tc ì 2 · 4 c N-×t`;
.>
Ci¬ tri ·o¬ r¬oiDo: <. :r¬oiDo .>
<FOLY>
<IT\L>

Kof qua fra vo nhu hình 1-11 khI frIou goI frang nay fron frình duyof.



CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM



Hình 1-11: Kof qua frang hoIIo.php

Iuu y rang. nou ban muon su dung scrIpf hay scrIpfIof nhu ASP fhì ban khaI bao
frong fap fIn php.InI nhu sau:

¬:r_t¬o: OD
; Aììcw A:J÷:tvì- <° °> t¬o:. .¬· ¹iDD ì¬ O11

KhI do frong frang PHP. fhay vì ban khaI bao

<.rDr
::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo``;
.>

Thì ban co fho khaI bao nhu sau:


::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo``;
°>

4.5. Gh¡ chu trong PHP
ChI chu frong kIch ban PHP fuong fu ngon ngu Iap frình C. do ghI chu mof dong
fhì ban su dung cap dau /. Chang han khaI bao sau Ia ghI chu:

<.rDr
KD¬i r¬c ri-D ¹- r¬oiDo
::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo``;
.>

Trong fruong hop co nhIou dong can ghI chu ban su dung cap dau /` va `/. ví du
khaI bao ghI chu nhu sau:

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

*
KD¬i r¬c ri-D ¹- ¹c· cd ìi-o
trcDo ¹c tct¬ìFcw: ì¬ ri-D tr¬
- tcDo :c .¬o tiD
*
:r-:oìt .v:cì_co-rv(::t:OL, :ìiD×);
:tct¬ìFcw:.v:cì_Do._rcw:(:r-:oìt);

NgoaI ra. ban cung co fho su dung dau # do khaI bao ghI chu cho fung dong. ví du
khaI bao sau Ia ghI chu:

<.rDr
= KD¬i r¬c ri-D ¹- r¬oiDo
::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo``;
.>

4.6. ¡n ket quu tren trung PHP
Khac vo cac kIch ban nhu ASP. JSP. PorI. doI voI PHP do In ra gIa frI fu bIon. bIou
fhuc. ham. gIa frI cu fho fhI ban co fho su dung scrIpf nhu fron:

Ci¬ tri ·o¬ r¬oiDo: <° :r¬oiDo °>

Tuy nhIon. do su dung cu phap cua PHP khI In ra gIa frI fu bIon. bIou fhuc. ham.
gIa frI cu fho fhì su dung khaI bao ocho nhu sau:

<.rDr
::t:OL:”:-ì-·t * 1rc. Co:tc.-r:”;
-·Dc ::t:OL:;
.>

Chang han. khaI bao ocho nhu ví du 1-3.
VI du 1-2: Trung echo.php

<IT\L>
<IFAL>
<TíTLF>::'-ì·c.- tc JIJ<TíTLF>
<IFAL>
<FOLY>
<.rDr
::ctr¬Do:r¬o-Do.r-r;
:r-·crc:rcwDo.r-r;
:tct“Fcw: D;
:r¬oiDo`Cc tc ì 2 · 4 c N-×t`;
*coDo rD¬t ri-o -·Dc *
-·Dc “Ci¬ tri ·o¬ r¬oiDo: “;
-·Dc :r¬oiDo;
.>
<FOLY>
<IT\L>

Kof qua fra vo nhu hình 1-12 khI frIou goI frang nay fron frình duyof.





CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Hình 1-11: Kof qua frang hoIIo.php

5. KKT L¡AN
Trong baI nay. chung fa fap frung fìm hIou cach caI daf PHP va Apacho Wob
Sorvor. sau do cau hình ung dung PHP frong IIS hay su dung cau hình mac dInh cua
chung.
NgoaI ra. ban Iam quon cach khaI bao ma PHP frong frang .php cung voI scrIpf hay
scrIpfIof.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mon hoc: PHP

Bu¡ 2

Bai boc nay cbung ta sc /am qucn oa tìm bicu cu pbap oa mot
so pbuong tbuc co ban cua PHP.

9 Cau /cnb.
9 Kicu Ju /icu oa bicn
9 Kbai bao oa su Jung bang.
9 Du /icu mang
9 Cbuycn Joi kicu Ju /icu
1. KHA¡ N¡KM VK C¡ PHAP PHP
Cu phap PHP chính Ia cu phap frong ngon ngu C. cac ban Iam quon voI ngon ngu C
fhì co IoI fho frong Iap frình PHP.
Ðo Iap frình bang ngon ngu PHP can chu y nhung dIom sau:
™ CuoI cau Ionh co dau ;
™ ÐIon frong PHP co fIon fo Ia $
™ MoI phuong fhuc dou baf dau { va dong bang dau }
™ KhI khaI bao bIon fhì khong co kIou du IIou
™ Non co gIa frI khoI dau cho bIon khaI bao
™ PhaI co chI chu |commonf) cho moI foafuro moI
™ Su dung dau // hoac # do gIaI fhích cho moI cau ghI chu
™ Su dung /` va `/ cho moI doan ghI chu
™ KhaI bao bIon co phan bIof chu hoa hay fhuong

2. KHA¡ BAO B¡KN
KhI fhuc hIon khaI bao bIon frong C. ban can phaI bIof fuan fhu quy dInh nhu: kIou
du IIou fruoc fon bIon va co gIa frI khoI dau. fuy nhIon khI Iam vIoc voI PHP fhì khong
can khaI bao kIou du IIou nhung su dung fIon fo $ fruoc bIon.
Xuaf phaf fu nhung dIou o fron. khaI bao bIon frong PHP nhu sau:
™ $varIabIonamo |=InIfIaI vaIuol;

$licount=0;
$lsSQL=”Select * from tblusers where active=1”;
$nameTypes = array("first", "last", "company");
$checkerror=false;
™ Chang han. khaI bao nhu ví du 2-1 |varIabIos.php)

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<BODY>
<h4>Variable</h4>
<?php
$sotrang=10;
$record=5;
$check = true;
$strSQL="select * from tblCustomers";
$myarr = array("first", "last", "company");
$myarrs[2];
$myarrs[0]="Number 0";
$myarrs[1]="Number 1";
$myarrs[2]="Number 2";
echo $myarr[1];echo "<br>";
echo $myarrs[2];
?>
</BODY>
</HTML>

3. K¡K¡ O¡ L¡K¡
Ðang cac kIou du IIou fhong fhuong
Booleun Truo hay faIso
¡nteger gIa frI Ion nhaf xap xI 2 fy
Flout ~1.8o308 gom 14 so Io
Str¡ng Iuu chuoI ky fu chIou daI vo han
Object KIou doI fuong
Arruy Mang voI nhIou kIou du IIou
3.1. Thuy do¡ k¡eu du l¡eu
Ðo fhay doI kIou du IIou. ban co fho su dung cach op kIou nhu frong cac ngon ngu Iap
frình C hay Java. Chang han. khaI bao op kIou nhu ví du 2-2 |box.php):

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Variable</h4>
<?php
$i="S10A";
echo $i+10;
echo "<br>";
$i="10A";
$j=(float)$i;
$j+=10;
echo $i;
echo "<br>";
echo $j;
echo "<br>";
$q=12;$p=5;
echo "Amount: ".(float)$q/$p;
?>
</BODY>
</HTML>

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Iuu y rang. PHP fu dong nhan bIof gIa frI chuoI dang sau so so khong duoc chuyon
sang kIou du IIou so nhu fruong hop fron.
NgoaI ra. ban co fho su dung ham soffypo do chuyon doI du IIou nay sag du IIou khac.
ví du chung fa khaI bao nhu ví du 2-3 |soffypo.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Change DataType of Variable</h4>
<?php
$var="12-ABC";
$check=true;
echo $var;
echo "<br>";
echo $check;
echo "<br>";
settype($var,"integer");
echo $var;
echo "<br>";
settype($check,"string");
echo $check;
?>
</BODY>
</HTML>


3.2. K¡em tru k¡eu du l¡eu cuu b¡en
Ðo kIom fra kIou du IIou cua bIon. ban su dung cac ham nhu sau:
Is_Inf do kIom fra bIon co kIou Infogor. nou bIon co kIou Infogor fhì ham so fra vo gIa
frI Ia fruo |1). Tuong fu. ban co fho su dung cac ham kIom fra fuong ung voI kIou du
IIou Ia Is_array. Is_booI. Is_caIIabIo. Is_doubIo. Is_fIoaf. Is_Inf. Is_Infogor. Is_Iong.
Is_nuII. Is_numorIc. Is_objocf. Is_roaI. Is_sfrIng. Chang han. ban khaI bao cac ham nay
nhu ví du 2-4 |chock.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Check DataType of Variable</h4>
<?php
$sotrang=10;
$record=5;
$check = true;
$strSQL="select * from tblCustomers";
$myarr = array("first", "last", "company");
$myarrs[2];
$myarrs[0]="Number 0";
$myarrs[1]="Number 1";
$myarrs[2]="Number 2";
echo is_array($myarr);
echo "<br>";
echo is_bool($record);
?>
</BODY>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

</HTML>

3.3.
3.4.
Thuy do¡ k¡eu du l¡eu b¡en
KhI khaI bao bIon va khoI fao gIa frI cho bIon voI kIou du IIou. sau do ban muon su
dung gIa frI cua bIon do fhanh fon bIon va co gIa frI chính Ia gIa frI cua bIon fruoc do
fhì su dung cap dau $$. Ví du. bIon $var co gIa frI Ia "fofaI". sau do muon su dung bIon
Ia fofaI fhì khaI bao nhu ví du 2-5 |chango.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Change DataType of Variable</h4>
<?php
$var="total";
echo $var;
echo "<br>";
$$var=10;
echo $total;

?>
</BODY>
</HTML>

K¡eu Arruy
KIou mang Ia mof mang so IIou do nguoI dung dInh nghIa. chung co cu phap nhu
sau:
$myarrs=array|"fIrsf". "Iasf". "company");
// mang bao gom cac kIou chuoI
hay co fho khaI bao nhu sau
$myarr|l=array|3);
$myarr|0l="Numbor 0";
$myarr|1l="Numbor 1";
$myarr|2l="Numbor 2";
Thu fu Indox frong mang baf dau fu vI frí 0. Chang han. ban khaI bao mang mof chIou
fhoo haI cach fron nhu ví du 2-6 |array.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Array on demenssion</h4>
<?php
$myarr[]=array(3);
$myarr[0]="Number 0";
$myarr[1]="Number 1";
$myarr[2]="Number 2";
echo $myarr[0];
echo $myarr[1];
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

echo $myarr[2];
echo "<br>";
$myarrs=array("first", "last", "company");
echo $myarrs[2];
?>
</BODY>
</HTML>

Nou nhu ban khaI bao mang haI chIou. fhì cu phap khaI bao nhu sau:
$myarrs|l|l=array|2.3);

Chang han khaI bao nhu ví du 2-7 |arrays.php):

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Array two demenssions</h4>
<?php
$myarrs[][]=array(2,3);
$myarrs[0][0]="Number 00";
$myarrs[1][0]="Number 10";
$myarrs[0][1]="Number 01";
$myarrs[1][1]="Number 11";
$myarrs[0][2]="Number 02";
$myarrs[1][2]="Number 13";
echo $myarrs[0][2];
echo "<br>";
?>
</BODY>
</HTML>

3.5. K¡eu do¡ tuong
Ðo khaI bao doI fuong. ban su dung khaI nIom cIass nhu frong ngon ngu Iap frình C
hay java. ngoaI ra phuong fhuc frong PHP duoc bIof don nhu mof ham. ÐIou nay co
nghIa Ia fu khoa Ia funcfIon.
Nou ham co fon frung voI fon cua cIass fhì ham do duoc goI Ia consfrucfor. Chang
han. chung fa khaI bao cIass va khoI fao chung fhì fu dong consfrucfor duoc goI moI
khI doI fuong khoI fao. sau do goI ham frong cIass do nhu ví du 2-8 |objocf.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Object</h4>
<?php
class clsA
{
function clsA()
{
echo "I am the constructor of A.<br />\n";
}
function B()
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

{
echo "I am a regular function named B in class A.<br />\n";
echo "I am not a constructor in A.<br />\n";
}
}
// Goïi phöông thöùc clsA() nhö constructor.
$b = new clsA();
echo "<br>";
// Goïi phöông thöùc B().
$b->B();
?>
</BODY>
</HTML>

3.6. Tum vuc cuu b¡en
Tam vuc cua bIon phu fhuoc vao noI khaI bao bIon. nou bIon khaI bao bon ngoaI ham
fhì so co fam vuc frong frang PHP. frong fruong hop bIon khaI bao frong ham fhì chI
co hIou Iuc frong ham do.
Ví du. chung fa co bIon $a khaI bao bon ngoaI ham nhung khI vao frong ham fhì bIon
$ duoc khaI bao IaI. bIon nay cofam vuoc bon frong ham. Tuong fu nhu vay. khI bIon
$I khaI bao frong ham fhì chI co fam vuc bon frong ham cho du chung duoc khaI bao
IaI bon ngoaI nhu ví du 2-9 |scopo.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Scope of Variable</h4>
<?php
$a = 100;
/* global scope */
function Test()
{
$i=10;
$a=10;
echo "<br>a:=$a";
echo "<br>i:=$i";
/* reference to local scope variable */
}
Test();
echo "<br>a:=$a";
$i=1000;
echo "<br>i:=$i";
?>
</BODY>
</HTML>

NgoaI ra. do su dung bIon foan cuc frong ham. ban su dung fu khoa gIobaI. khI do bIon
foan cuc so co hIou Iuc bon frong ham. Ví du khaI bao bIon $a bon ngoaI ham. sau do
bon frong ham Tosf ban su dung fu khoa gIobaI cho bIon $a. khI do bIon $a so duoc su
dung va gIa frI do co hIou Iuc sau khI ra khoI ham chu khong gong nhu fruong hop
frong ví du scopo.php nhu ví du 2-10 |gIobaI.php).

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

</HEAD>
<BODY>
<h4>Scope of Variable</h4>
<?php
$a = 100;
/* global scope */
function Test()
{
global $a;
$i=10;
$a+=10;
echo "<br>a:=$a";
echo "<br>i:=$i";
/* reference to local scope variable */
}
Test();
echo "<br>a:=$a";
$i=1000;
echo "<br>i:=$i";
?>
</BODY>
</HTML>

4. HANG TRONG PHP
4.1.
4.2.
Khu¡ buo vu su dung hung
Hang Ia gIa frI khong fhay doI ko fu sau khI khaI bao. ban co fho su dung phaf bIou
ÐofIno do khaI bao hang nhu sau:

define("MAXSIZE", 100);

Ðo su dung hang. ban khaI bao nhu ví du 2-11 |consfanf.php)

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Constant</h4>
<?php
define("pi",3.14);
function Test()
{
echo "<br>pi:=".pi;
echo "<br>pi:=".constant("pi");
}
Test();
echo "<br>pi:=".pi;
echo "<br>pi:=".constant("pi");
?>
</BODY>
</HTML>

K¡em tru hung
KhI su dung hang. ma hang chua fon faI fhì ban su dung ham dofInod nhu ví du 2-
12 sau |dofaInod.php):

<HTML>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Constant</h4>
<?php
define("pi",3.14);
//define("hrs",8);
function Test()
{
if(defined("pi"))
echo "<br>pi:=".pi;
else
echo "<br>pi not defined";
if(defined("hrs"))
echo "<br>hrs:=".hrs;
else
echo "<br>hrs not defined";
}
Test();
?>
</BODY>
</HTML>


5. KKT L¡AN
Trong baI nay. chung fa fìm hIou cach khaI bao hang. bIon va su dung hang bIon. NgoaI
ra. ban cung fìm hIou cach chuyon doI kIou du IIou. kIom fra kIou du IIou. fam vuoc cua
bIon.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
Baøi 3
PHKP TOAN VA PHAT B¡K¡ CO O¡K¡ K¡KN
TRONG PHP

Chöông naøy chuùng ta seõ laøm quen vaø tìm hieåu toaùn töû, phaùt bieåu
coù ñieàu kieän vaø voøng laëp cuûa PHP.

Nhöõng vaán ñeà chính seõ ñöôïc ñeà caäp trong baøi hoïc naøy
9 Toaùn töû.
9 Pheùp gaùn trong PHP
9 Phaùt bieåu coù ñieàu khieån.
9 Voøng laëp.


1. KHA¡ N¡KM VK CAC TOAN T¡ TRONG PHP
KhI ban Iap frình fron PHP Ia su dung cu phap cua ngon ngu C. C++.
Tuong fu nhu nhung ngon ngu Iap frình khac. foan fu gIup cho ban fhuc hIon
nhung phop foan nhu so hoc hay fron chuoI.
Ðang sau day gIup cho ban hình dung duoc nhung foan fu su dung frong
PHP. PHP dInh nghIa foa fu foan hoc. quan ho. so hoc. bIf va nof so phop foan
gan.

IoaI foan fu Toan fu ÐIon gIaI Ví du
ArIfhmofIc
+
-
*
/
%
AddIfIon
SubfracfIon
MuIfIpIIcafIon
ÐIvIsIon
ModuIus
a + b
a - b
a * b
a / b
a % b
RoIafIonaI
>
<
>=
<=
!=
==
Croafor fhan
Ioss fhan
Croafor fhan or oquaI
Ioss fhan or oquaI
Nof oquaI
ÐquaI
a > b
a < b
a >= b
a <= b
a != b
a == b
IogIcaI
!
&&
||
Nof
ANÐ
OR
!a
a && b
a || b
huukhung©yuhoo.com 3-1
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
AssIgnmonf

=
++
--
+=
-=
`=
/=
%=
1=
&=
^=
.=

Incromonf and assIgn
Ðocromonf and assIgn
Add and assIgn
Subfracf and assIgn
MuIfIpIy and assIgn
ÐIvIdo and assIgn
Tako moduIus and assIgn
OR and assIgn
ANÐ and assIgn
XOR and assIgn
Concaf and assIgn

a = b
a++
a--
a += b
a -= b
a *= b
a /= b
a %= b
a |= b
a &= b
a ^= b
a .= b
AIIocafIon
new
Croafo a nov objocf of a
cIass
new A()
SoIocfIon
? :
If...Thon soIocfIon
a ? b : c

2. G¡O¡ TH¡K¡ TOAN T¡
KhI noI don foan fu. chung fa Iuon IIon fuong don fhu fu xu Iy. cung nhu
frong foan hoc. foan fu frong PHP cung co do uu fIon add-subfracf-muIfI-dIvIdo.
2.1. Toun tu ANO
KhI fhuc hIon mof vIoc fang Ion gIa frI fhì ban su dung cu phap nhu sau:
$ I=0;$j=0;
j=I++;// I fang sau khI gan I vao j. chính vì vay sau khI gan I vao j. j van
khong fhay doI
j=++I;// I fang fruoc khI gan I vao j. chính vì vay sau khI gan I vao j. j fhay
doI.

VI du 3.1: Phep toun ANO.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>AND Operator</h4>
<?php
huukhung©yuhoo.com 3-2
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
$i=10;
$j=5;
$j+=$i++;
echo "j=$j";
echo "<br>";
echo "i=$i";
echo "<br>";
$j+=++$i;
echo "j=$j";echo "<br>";
?>
</BODY>
</HTML>
2.2. Toun tu Not: ~ And !
Toan fu ~ dao nghIch faf ca cac bIf cua fham so. con foan fu ! dao nghIch
gIa frI cua gIa frI fruoc do. Chang han frong fruong hop nay chung fa su dung
cho bIou fhuc hay bIon co gIa frI booIoan.
VI du 3.2: Phep toun ~ und !

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>~, ! Operator</h4>
<?php
$i=10;
$j=5;
$j+=~$i;
echo "j=$j";
echo "<br>";
$j+=~$i++;
echo "i=$i";
echo "<br>";
$j+=++$i;
echo "j=$j";
echo "<br>";
?>
</BODY>
</HTML>

2.3. Toun tu nhun vu ch¡u: * und /
Ðan co fho fham khao ví du sau
VI du 3.3: Phep toun * vu /, + vu -


<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
huukhung©yuhoo.com 3-3
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
</HEAD>
<BODY>
<h4>Multi And Divide Operator</h4>
<?php
$i=10;
$j=5;
echo $i/$j;
echo "<br>";
echo $i*$j;
?>
</BODY>
</HTML>

2.4. Toun tu modulus: %
KhI chIa mof so cho mof so. ban can kof qua Ia so du cua phop chIa do fhì
dung foan fu moduIus
VI du 3.4: Phep toun %


<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Mod Operator</h4>
<?php
$i=10;
$j=7;
echo $i%$j;
echo "<br>";
?>
</BODY>
</HTML>

2.5. Toun tu quun he: >=,>,<,<=,==,!=
KhI can so sanh kof qua gIua haI foan han voI nhau. fhong fhuong ban nghI
don phop foan so sanh nhu Ia bang. Ion hon. nho hon. ví du sau dIon gIaI cho
ban cac foan fu fron:
VI du 3.5: Phep toun >,>=,<,<=,==,!=


<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Comparation Operators</h4>
<?php
huukhung©yuhoo.com 3-4
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
$i=10;
$j=9;
echo $i<$j;
echo "<br>";
echo $i!=$j;
?>
</BODY>
</HTML>

2.6. Toun tu && vu 11
&& Ia foa fu and frong so hoc. 11 Ia foan fu or frong so hoc. HaI foan fu
nay raf fhuong dung frong khI Iap frình fron PHP. ví du duoI day dIon gIaI cho
ban day du haI foan fu nay. Chu y rang khI su dung foan fu dou co kom phaf
bIou co dIou kIon.
VI du 3.6: Phep toun && vu 11


<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Logic Operators</h4>
<?php
$b=true;
$j=3;
if (($j>=3) && ($b!=true))
{
echo "result is true";
}
if(($j<3) || ($b==true))
echo "result is false";
?>
</BODY>
</HTML>

2.7. Toun tu ?:
Toan fu nay fhay fho cho phaf bIou co dIou kIon If...oIso. khI ban can Iay
kof qua fhoo dIou kIon nao do. nou co fho khong can phaf bIou If-oIso. fhì hay
fhay fho bang foan fu 7:. cu phap cua chung nhu sau:
sfr1=sfr2.oquaIs|”khang”)7”WoIcomo fo PHP”:”Cood byo PHP”;

VI du 3.?: Phep toun ?:


<HTML>
huukhung©yuhoo.com 3-5
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Selection Operators</h4>
<?php
$str1="Pham Huu Khang";
$str2 ="Khang";
$str1=(str1==str2)?"Welcome to PHP":"Good bye PHP";
echo "result is ".$str1;
?>
</BODY>
</HTML>

3. PHKP GAN
KhI gan mof gIa frI hay bIon vao mof bIon frong PHP. ban phaI dung don
phop gan. nhung frong PHP cung gIong nhu frong C fhì co nhung phop gan duoc
don gIan hoa hay noI dung hon Ia chuan hoa do ruf gon IaI frong khI vIof.
3.1.
3.2.
3.3.
3.4.
4.
Phep gun thong thuong nhut nhu suu:
$j=I;
$sfr1 =” HoIIo!”;
$b=fruo;
Phep gun them mot g¡u trj lu 1
$k=0;
$k++;
Phep gun chuo¡
$sfrX="HoIIo";
$sfrX.=” vorId”;
$sfrX.=”AÐCc”.$x;
Phep gun them mot vo¡ chInh no g¡u trj
$k=0;$j=1;
$k+=$j;
fuong fu nhu vay chung fa co $k`=2. nghIa Ia $k=$k`2
PHAT B¡K¡ CO O¡K¡ K¡KN
Cac phaf bIou co dIou kIon nhu :
™ II |dIou kIon) { cau Ionh; }
™ II |dIou kIon) { cau Ionh; }ELSE { cau Ionh; }
huukhung©yuhoo.com 3-6
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
™ II |dIou kIon) { cau Ionh; }ELSEIF { cau Ionh; }
™ svIfch |dIou kIon)
{
caso VaIuo1
cau Ionh1;
broak;
}
™ WhIIo |dIou kIon)
™ Ðo - WhIIo |dIou kIon)
™ Ðroak
™ ConfInuo

4.1. Phut b¡eu ¡F (d¡eu k¡en) { cuu lenh: }
Su dung phaf bIou If do chon Ioc kof qua khI dIou kIon dung. ví du nhu sau:
VI du 3.8: Phut b¡eu ¡F

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>IF Statement</h4>
<?php
$b=true;
$j=3;
if(($j>=3) &&($b!=true))
echo "result is true";
if(($j<3) ||($b==true))
echo "result is false";

?>
</BODY>
</HTML>

4.2. Phut b¡eu ¡F (d¡eu k¡en) { cuu lenh: }ELSE { cuu lenh: }
Su dung phaf bIou If do chon Ioc kof qua khI dIou kIon dung. va xuaf ra kof
qua khI dIou kIon saI. ví du nhu sau:
VI du 3.9: Phut b¡eu ¡F - KLSK

<HTML>
<HEAD>
huukhung©yuhoo.com 3-?
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>IF ELSE Statement</h4>
<?php
$b=true;
$j=3;
if ($j>3)
echo "result is true";
else
{
$j++;
echo "result is $j";
}
?>
</BODY>
</HTML>
4.3.
4.4.
Phut b¡eu KLSK¡F
Phaf bIou oIsoIf Ia phan cua phaf bIou If oIso nhIou nhanh. khI co nhIou
dIou kIon chon Iua fhì ban su dung oIsoIf. cu phap cua chung nhu sau:

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>ELSEIF Statement</h4>
<?php
$b=true;
$j=3;
if ($j>3)
echo "result is true";
elseif ($j=0)
{
$j++;
echo "result is $j";
}
else
{
$j--;
echo "result is ". $j--;
}
?>
</BODY>
</HTML>

Phut b¡eu Sw¡tch (d¡eu k¡en)
Phaf bIou svIfch Ia phan cua phaf bIou oIsoIf nhIou nhanh. khI co nhIou
dIou kIon chon Iua fhì ban su dung svIfch. cu phap cua chung nhu sau:
huukhung©yuhoo.com 3-8
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
SvIfch|dIou kIon)
{
caso VaIuo1
cau Ionh1;
broak;
caso VaIuo2
cau Ionh2;
broak;

dofauIf:
cau Ionh dofauIf;
}
Ðroak: dung do fhoaf ra khoI svIfch khI fhoa mof caso nao do frong svIfch.
dofauIf: khI khong co baf ky gIa frI nao fhoan frong cac caso fhì gIa frI cuoI cung
Ia dofauIo sfafomonf
VI du 3.10: Phut b¡eu Sw¡tch

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>SWITCH Statement</h4>
<?php
$j=3;
$j=date("w");
$str="";
switch($j)
{
case 0:
$str="Today is Sunday";
break;
case 1:
$str="Today is Monday";
break;
case 2:
$str="Today is Tueday";
break;
case 3:
$str="Today is Wednesday";
break;
case 4:
$str="Today is Thursday";
huukhung©yuhoo.com 3-9
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
break;
case 5:
$str="Today is Friday";
break;
case 6:
$str="Today is Saturday";
break;
default:
$str="Today is Sunday";
break;
}
echo $str;
?>
</BODY>
</HTML>


4.5. Phut b¡eu Wh¡le(d¡eu k¡en)
Phaf bIou vhIIo fhuc fhI nhung cau Ionh frong vhIIo khI dIou kIon co gIa frI
fruo.
VI du 3.11: Phut b¡eu Wh¡le


<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>While Statement</h4>
<?php
$j=10;
while($j>0)
{
echo $j."<br>";
$j--;
}
?>
</BODY>
</HTML>


4.6. Phut b¡eu For
Phaf bIou for dung cho vong Iap co gIoI han cho fruoc. cu phap co dang nhu
sau:

VI du 3.12: Phut b¡eu For

<HTML>
<HEAD>
huukhung©yuhoo.com 3-10
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>FOR Statement</h4>
<?php
for($j=1;$j<=10;$j++)
{
echo $j."<br>";
}
?>
</BODY>
</HTML>


4.7. Phut b¡eu do wh¡le
Phaf bIou do vhIIo cho phop duyof va kIom fra dIou kIon sau phaf bIou fhu
nhaf. dIou nay co nghIa Ia íf nhaf mof phaf bIou duoc fhuc hIon.
VI du 3.13: Phut b¡eu Oo Wh¡le

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Do While Statement</h4>
<?php
$j=10;
do
{
echo $j."<br>";
$j--;
}while($j>0)
?>
</BODY>
</HTML>


Phaf bIou oxIf cho phop fhoaf ra khoI phaf bIou dIou kIon khI fhoa dIou
kIon nao do.
VI du 3.14: Phut b¡eu ex¡t

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Exit Statement</h4>
<?php
$j=10;
do
huukhung©yuhoo.com 3-11
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM
{
if($j==3) exit;
echo $j."<br>";
$j--;
}while($j>0)
?>
</BODY>
</HTML>

TOM TAT 5.
Trong baI hoc nay chung foI gIoI fhIou don cho cac ban cac phop gan. cac foan
fu. dong fhoI gIup cho cac ban hIou fhom vao cac phaf bIou co dIou kIon nhu
vhIIo. for. svIfch. ….
huukhung©yuhoo.com 3-12
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mon hoc: PHP

Bu¡ 4

Baøi hoïc naøy chuùng ta seõ laøm quen vôùi bieán form vaø hai phöông
thöùc $HTTP_POST_VARS vaø $HTTP_GET_VARS cuûa PHP:

9 Bieán form.
9 Phöông thöùc $HTTP_GET_VARS
9 Phöông thöùc $HTTP_POST_VARS


B¡KN FORM
ÐIon form frong PHP duoc bIof don nhu mof IoaI bIon. fhay vì khaI bao fhì bIon do
chính Ia fon cua fho nhap IIou frong frang submIf hay fham so fron quorysfrIng.
1.
1.1. B¡en Iorm tu Iorm duoc subm¡t vo¡ phuong thuc POST
Trong frang ban submIf don. nou khaI bao fon cua fho nam frong fho form co fon Ia
xyz fhì bIon form duoc dInh nghIa Ia $xyz.
Chang han. ban khaI bao bao fho form frong frang submIf.php nhu ví du 4-1.
VI du 4-1: Khu¡ buo the Iorm

...
<form action=ex1-1.php method=post>
<tr>
<td>Name</td><td>:<input type=text name=fullname></td>
</tr>
<tr><td>Gender</td>
<td>:<input type=radio value=M name=gender> Male
<input type=radio value=F name=gender> Female</td>
</tr>
<tr><td>&nbsp</td>
<td><input type=submit value=Submit></td>
</tr>
</form>
...

KhI nguoI su dung nhap gIa frI vao phan Namo va chon gIoI fính MaIo hay IomaIo
nhu hình 4-1. nou nhan nuf submIf fhì frang ox1-1.php so frIou goI. frong frang nay
ban co fho Iay gIa frI nhap fu frang ox1.php bang cach su dung bIon form nhu ví du
4-1-1.
VI du 4-2: Oung b¡en Iorm

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Form variable</h4>
<table>
<tr><td>Name</td>
<td>
:<?=$fullname?>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

</td></tr>
<tr><td>Gender</td>
<td>
:<?=$gender?>
</td></tr>
</table>
</BODY>
</HTML>

Trong do. $fuIInamo va $gondor Ia fon cua haI fho Inpuf frong frang ox1.php. frong
fruong hop nay chung fa su dung phuong fhuc POST cho form.

Hình 4-1: Nhap IIou

Kof qua fra vo nhu hình 4-1-1.

Hình 4-1-1: Kof qua Iay fu frang submIf bang bIon form
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

1.2. B¡en Iorm tu Iorm duoc subm¡t vo¡ phuong thuc GKT
Nou ban su dung phuong fhuc CÐT frong fho form. ban co fho Iay gIa frI cua cac
fham so fron chuoI QuorySfrIng bang bIon form. Ví du khaI bao fho form co haI fuy
chon nhu ví du 4-2 voI phuong fhuc CÐT frong fho form.
VI du 4-2: Khu¡ buo the Iorm

...
<form action=ex2-1.php method=get>
<tr><td>Province</td>
<td>
:<select name=province>
<option value=HAN>Ha Noi</option>
<option value=HCM>Ho Chi Minh</option>
<option value=HUE>Hue</option>
</select>
</td></tr>
<tr><td>Industry</td>
<td>
:<select name=industry multiple>
<option value=AUT>Automobile</option>
<option value=FOO>Foods</option>
<option value=ENG>Enginering</option>
<option value=GAR>Garment</option>
</select>
</td></tr>
<tr><td>&nbsp</td>
<td><input type=submit value=Submit></td></tr>
</form>
...

KhI frIou goI frang ox2.php fron frình duyof. nguoI su dung chon gIa frI frong haI
fuy chon ProvInco va Indusfry nhu hình 4-2.


Hình 4-2: Phuong fhuc CÐT
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Nou nhan SubmIf fhì haI gIa frI chon so duoc fruyon Ion fron QuorySfrIng voI haI
fham so Ia fon cua fho soIocf. Ví du frong fruong hop nay kof qua fra vo nhu hình 4-
2-1.



Hình 4-2-1: ÐIon form voI phuong fhuc CÐT
Trong do. haI fham so va gIa frI fuong ung Ia ox2-
1.php7provInco=HAN&Indusfry=IOO. bang cach su dung bIon form ban co fho Iay
duoc gIa frI nay nhu ví du 4-2-1.
VI du 4-2-1: Khu¡ buo the Iorm

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Form variable</h4>
<table>
<tr><td>Province</td>
<td>
:<?=$province?>
</td></tr>
<tr><td>Industry</td>
<td>
:<?=$industry?>
</td></tr>
</table>
</BODY>
</HTML>

ÐoI voI fruong hop ban khong su dung fho form nhu haI fruong hop fron. chung fa
cung co fho Iay gIa frI fu chuoI QuorySfrIng bang bIon form. Chang han. ban khaI
bao frang chop phop nguoI su dung chon ky fu do IIof ko danh sach khach hang fhoo
ky fu do nhu hình 4-3.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM




Hình 4-3: Chon ky fu
Ðang cach khaI bao cac fho <a> ban dInh nghIa 24 ky fu nhu hình fron voI fham so
aI co gIa frI fuong ung:

<tr><td>
<a href="ex3.php?al=A">A</a> |
<a href="ex3.php?al=B">B</a> |
<a href="ex3.php?al=C">C</a> |
<a href="ex3.php?al=D">D</a> |
<a href="ex3.php?al=E">E</a> |
<a href="ex3.php?al=">All</a>
</td></tr>

KhI nguoI su dung chon mof ky fu fhì su dung bIon form Ia fon cua fham so |aI). ban
co fho Iay duoc gIa frI cua ky fu dang chon:

<tr><td>Select:<?=$al?></td></tr>

Tuy nhIon. Ian dau fIon frIou goI frang nay ma khong co fham so fron QuorySfrIng.
khaI bao bIon form so phun ra IoI nhu hình 4-3-1.
Ðo franh fruong hop nay. ban su dung ham Issof do kIom fra bIon fon faI hay
khong. nou fon faI fhì ban su dung bIon form nay. Ví du doI voI fruong hop nay
chung fa khaI bao nhu ví du 4-3.
VI du 4-3: Su dung b¡en Iorm

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Form variable</h4>
<table>
<tr><td>List by Alphabet</td></tr>
<tr><td>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<a href="ex3.php?al=A">A</a> |
<a href="ex3.php?al=B">B</a> |
<a href="ex3.php?al=C">C</a> |
<a href="ex3.php?al=D">D</a> |
<a href="ex3.php?al=E">E</a> |
<a href="ex3.php?al=">All</a>
</td></tr>
<?php
if(isset($al))
{
?>
<tr><td>Select:<?=$al?></td></tr>
<?php
}
?>
</table>
</BODY>
</HTML>



Hình 4-3-1: IoI phaf sInh
Chu y rang. khI su dung bIon form ban khong non khaI bao bIon cung fon voI cac
fham so hay fon cua fho nhap IIou frong frang frIou goI fruoc do. Nou khong fhì gIa
frI fra vo Ia gIa frI cua bIon fhuong fhay vì bIon form.
2. PH¡ONG TH¡C $HTTP_GKT_VARS
NgoaI cach su dung bIon form frong fruong hop Iay gIa frI fu fham so cua QuorySfrIng.
ban co fho su dung ham $HTTP_CÐT_VARS. Ví du. chung fa khaI bao frang PHP nhu
ví du 4-4.
VI du 4-4: Su dung $HTTP_GKT_VARS

<HTML>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Form variable</h4>
<table>
<form action=ex4.php method=get>
<tr><td>Province:<select name=province>
<option value=HAN>Ha Noi</option>
<option value=HCM>Ho Chi Minh</option>
<option value=HUE>Hue</option>
</select>
<input type=submit value=Submit></td></tr>
</form>
<tr><td>
<?php
if(isset($HTTP_GET_VARS["province"]))
{
$result=$HTTP_GET_VARS["province"];
echo "Result: ".$result;
}
?>
</td></tr>
</table>
</BODY>
</HTML>

Iuu y rang. nou ban khong su dung ham Issof do kIom fra provInco fon faI hay khong
fhì frang php so phun IoI frong fruong hop Ian dau fIon goI don frang ox4.php ma
khong submIf. Tuy nhIon. nou ban submIf frang nay fhì kof qua fra vo nhu hình 4-4.


Hình 4-4: Ðung $HTTP_CÐT_VARS
Tuong fu nhu vay frong fruong hop ban khong su dung fho form ma gIa frI Iay fu chuoI
QuorySfrIng bang cach su dung $HTTP_CÐT_VARS nhu ví du 4-5.
VI du 4-5: Su dung $HTTP_GKT_VARS

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<BODY>
<h4>Form variable</h4>
<table>
<tr><td>List by Alphabet</td></tr>
<tr><td>
<a href="ex3.php?al=A">A</a> |
<a href="ex3.php?al=B">B</a> |
<a href="ex3.php?al=C">C</a> |
<a href="ex3.php?al=D">D</a> |
<a href="ex3.php?al=E">E</a> |
<a href="ex3.php?al=">All</a>
</td></tr>
<?php
if(isset($HTTP_GET_VARS["al"]))
{
?>
<tr><td>Select:<?=$HTTP_GET_VARS["al"]?></td></tr>
<?php
}
?>
</table>
</BODY>
</HTML>

Kof qua fra vo nhu hình 4-5.



Hình 4-5: Su dung $HTTP_CÐT_VARS
3. PH¡ONG TH¡C $HTTP_POST_VARS
Tuong fu nhu $HTTP_CÐT_VARS nhung $HTTP_POST_VARS cho phop ban Iay gIa frI
Iay fu cac fho nhap IIou cua fho form frong fraang submIf fruoc do. Ví du. ban khaI bao
frang nhap IIou nhu ví du 4-6.
VI du 4-5: Khu¡ buo Iorm vo¡ phuong thuc POST

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<BODY>
<h4>Form variable</h4>
<form action=ex7.php method=post>
<table>
<tr><td>User Name</td>
<td>
:<input type=text name=username>
</td></tr>
<tr><td>Password</td>
<td>
:<input type=password name=password>
</td></tr>
<tr><td>&nbsp</td>
<td><input type=submit value=Login></td></tr>
</table>
</form>
</BODY>
</HTML>

KhI nguoI su dung nhap usornamo va passvord nhu hình 4-6 va nhan nuf IogIn.



Hình 4-6: Ðang nhap
Ðang cach su dung $HTTP_POST_VARS do Iay gIa frI usornamo va passvord nhu ví du
4-7.
VI du 4-5: Su dung $HTTP_POST_VARS

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Form variable</h4>
<?php
if(isset($HTTP_POST_VARS["username"]))
{
?>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<table>
<tr><td>Username</td>
<td>:<?=$HTTP_POST_VARS["username"]?></td></tr>
<tr><td>Password</td><td>
:<?=$HTTP_POST_VARS["password"]?></td></tr>
</table>
<?php
}
?>
</BODY>
</HTML>

Kof qua frình bay nhu hình 4-7.



Hình 4-7: Ðung $HTTP_POST_VARS
4. KKT L¡AN
Trong baI nay. chung fa fìm hIou cach su dung bIon form va haI phuong fhuc
$HTTP_POST_VARS. $HTTP_CÐT_VARS. NgoaI ra. ban cung fìm hIou cach kIom fra
bIon fon faI hay khong bang ham Issof|).
Chu y rang. khI su dung bIon form ban franh fruong hop khaI bao bIon cuc bo hay foan
cuc frong fang PHP cung fon voI fho nhap IIou cua form fruoc do submIf don hay fham
so fron quorysfrIng.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mon hoc: PHP

Bu¡ 5

Baøi hoïc naøy chuùng ta seõ laøm quen vôùi ñoái töôïng Session vaø moät
soá ñoái töôïng khaùc:

9 Ñoái töôïng Session.
9 Ñoái töôïng khaùc


OO¡ T¡ONG SKSS¡ON
Trong PHP4.0 doI fuong SossIon duoc xom nhu mof doI fuong cho phop ban fruyon gIa
frI fu frang PHP nay sang PHP khac. Ðo su dung SossIon. ban khaI bao fhu muc duoc
Iuu fru du IIou do doI fuong nay ghI ra.
SossIon duoc sInh ra va duoc bIon maf khI nguoI su dung huy chung. fhoI gIan song cua
chung da hof hoac nguoI su dung dong frình duyof.
Chang han. frong fruong hop nay chung fa su dung fhu muc C:`PHP`sossIondafa duoc
khaI bao frong fap fIn php.InI.
1.

session.save_path = C:\PHP\sessiondata

NgoaI ra. khI muon su dung SossoIn fhì ban phaI khoI fao chung. Ðo khoI fao SossIon
ban co fho khoI fao frong frang PHP moI khI fruy cap hay gan gIa frI cho SossIon.

session_start();

Tuy nhIon. ban co fho cau hình frong frang php.InI |1 Ia sfarf).

session.auto_start = 0

1.1. Nhun dung Sess¡on
MoI phIon Iam vIoc duoc fao ra fu Wob Sorvor fhì so co mof nhan dang duy nhaf co
gIa frI Ia chuoI do frình chu Wob fao ra. ÐIou nay co nghIa Ia moI khI nguoI su dung
frIou goI frang Wob cua Wob SIfo Ian dau fIon fhì phIon Iam vIoc so duoc fao ra. khI
do mof nhan dang duoc cap cho phIon Iam vIoc do.
Ðo Ian gIa frI nhan dang cua SossIon do frình chu Wob cap phaf ban su dung cu
phap:

$x= session_id();

Chang han. ban khaI bao bao do Iay gIa frI sossIon_Id frong frang sossIonId.php nhu
ví du 5-1.
VI du 5-1: Nhun dung sess¡on

<?php
session_start();
?>
<HTML>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Session_ID</h4>
<?php
$sessionid=session_id();
echo $sessionid;
?>
</BODY>
</HTML>


MoI nguoI su dung fruy cap don Wob SIfo so co mof nhan dang khac nhu nhu hình 5-
1.



Hình 5-1: Nhan dang duy nhaf
1.2. Khu¡ buo Sess¡on
KhI muon khaI bao bIon sossIon. ban phaI su dung ham sossIon_rogIsfor co cu phap
nhu sau:

session_register("sessioname");

KhI muon khoI fao sossIon. ban co fho gan gIa frI cho sossIon nay nhu gan gIa frI
cho bIon frong PHP. sau do su dung ham fron do dang ky.

$sessioname=value;
session_register("sessioname");

Trong fruong hop co nhIou sossIon. ban co fho su dung ham sossIon_rogIsfor do dang
ky cung mof Iuc nhIou sossIon nhu sau:

$sessioname1=value1;
$sessioname2=value2;
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

$sessioname3=value3;
session_register("sessioname1","sessioname2","sessioname3");

Chang han. frong fruong hop nay chung fa khaI bao frang sossIonrogIsfor.php va
dang ky 3 sossoIn co fon usorId. omaII va fuIInamo nhu ví du 5-2 sau:
VI du 5-2: Oung ky sess¡on

<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Registered Session</h4>
<?php
$userid="123";
$email="test@yahoo.com";
$fullname="Nguyen Van Ba";
session_register("userid");
session_register("email","fullname");
?>
</BODY>
</HTML>

Kof qua fra vo nhu hình 5-2.



Hình 5-2: Ðang ky SossIon
1.3. Luy g¡u trj tu sess¡on
Sau khI khaI bao khoI ao mof so sossIon voI gIa frI fuong ung cua sossIon do. ban co
fho fruy cap cac bIon sossIon nay do Iay gIa frI forng frang PHP khac. Chang han.
chung fa khaI bao frang gofsossIon.php do Iay cac sossIon cua PHP vua khaI bao
frong ví du fron nhu ví du 5-3.
VI du 5-3: Luy g¡u trj tu sess¡on
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Get Session</h4>
<?php
echo "UserID: ". $userid."<br>";
echo "Email: ".$email."<br>";
echo "FullName: ".$fullname;
?>
</BODY>
</HTML>


KhI frIou goI frang gofsossIon.php fron frình duyof ban frình bay gIa frI cua sossIon
usorId. omaII va fuIInamo nhu hình 5-3.



Hình 5-3: Iay gIa frI cua sossIon
Tuy nhIon. frong fruong hop ban fruy cap mof bIon sossIon chua khoI fao fruoc do
fhì I64I so phaf sInh. Ví du frong fruong hop nay chung fa fruy cap bIon sossIon co
fon $addross nhu ví du 5-4.
VI du 5-4: Truy cup sess¡on chuu ton tu¡

<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<h4>Get Session</h4>
<?php
echo "UserID: ". $userid."<br>";
echo "Email: ".$email."<br>";
echo "FullName: ".$fullname;
echo "Address: ".$address;
?>
</BODY>
</HTML>

KhI frIou goI frang unrogIsforsossIon.php fron frình duyof fhì IoI phaf sInh nhu hình
5-4.



Hình 5-4: IoI phaf sInh
Ðo kIom fra sossIon do co fon faI hay chua ban su dung ham sossIon_Is_rogIsfor
frong frang chocksossIon.php. ÐoI voI fruong hop nay chung fa can kIom fra 4
sossIon fruoc khI fruy cap don chung nhu ví du 5-5.
VI du 5-5: K¡em tru sess¡on

<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Get Session</h4>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<?php
if(session_is_registered("userid"))
echo "UserID: ". $userid."<br>";
if(session_is_registered("email"))
echo "Email: ".$email."<br>";
if(session_is_registered("fullname"))
echo "FullName: ".$fullname;
if(session_is_registered("address"))
echo "Address: ".$address;
?>
</BODY>
</HTML>

KhI frIou goI frang chocksossIon.php fhì kof qua so frình bay nhu hình 5-5.



Hình 5-5: Khong co IoI phaf sInh
Chu y rang. khI su dung don sossIon. ban phaI khoI dong chung bang sossIon_sfarf|)
nou khong fhì phaI khaI bao frong php.InI.
1.4. Huy sess¡on
KhI khong co nhu cau su dung sossIon nua fhì ban su dung ham sossIon_unrogIsfor
do IoaI sossIon do. Chang han. frong fruong hop nay chung fa muon IoaI bo sossIon
co fon Ia fuIInamo ban khaI bao frong frang sossIonunrogIsfor.php nhu ví du 5-6.
VI du 5-6: Lou¡ bo mot Sess¡on

<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<h4>UnRegister Session</h4>
<?php
session_unregister("fullname");
if(session_is_registered("userid"))
echo "UserID: ". $userid."<br>";
if(session_is_registered("email"))
echo "Email: ".$email."<br>";
if(session_is_registered("fullname"))
echo "FullName: ".$fullname;
if(session_is_registered("address"))
echo "Address: ".$address;
?>
</BODY>
</HTML>

KhI frIou goI frang sossIonunrogIsfor.php fron frình duyof fhìkof qua fra vo nhu hình 5-
6.


Hình 5-6: IoaI bo sossIon
Trong fruong hop IoaI bo faf cac sossIon dang fon faI fhì su dung ham sossIon_unsof|).
Ví du dung ham nay do IaoI bo sossIon va dung ham sossIn_dosfroy do huy faf ca
sossIon do khaI bao frong frang unsof.php nhu ví du 5-7.
VI du 5-?: Xou tut cu sess¡on

<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>UnSet Session</h4>
<?php
session_unset();
session_destroy();
if(session_is_registered("userid"))
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

echo "UserID: ". $userid."<br>";
if(session_is_registered("email"))
echo "Email: ".$email."<br>";
if(session_is_registered("fullname"))
echo "FullName: ".$fullname;
if(session_is_registered("address"))
echo "Address: ".$address;
?>
</BODY>
</HTML>


Kof qua fra vo nhu hình 5-7.



Hình 5-7: Huy sossIon
2. COOK¡K
CookIo duoc xom nhu sossIon. fuy nhIon chung Iuu fru fhong fIn fron frình khach. Ðo
su dung CookIo. ban su dung ham sofcookIo do gan gIa frI nhu ví du 5-8.
VI du 5-8: Gun g¡u trj cho cook¡e

<?php
setcookie("huukhang","Computer Learning Center");
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Cookie</h4>
</BODY>
</HTML>


KhI nguoI su dung frIou goI frang sofcookIo.php kof qua fra vo nhu hình 5-8.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM



Hình 5-8: Ðang ky cookIo
NgaI ra. ban co fho gan gIa frI cookIo bang sossIon. Chang han. chung fa su dung ham
sossIon_sof_cookIo_params do gan cookIo nhu ví du 5-9.
VI du 5-9: Gun cook¡e bung sess¡on

<?php
session_start();
$myvalue="Online Recruitment";
session_set_cookie_params($myvalue);
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Session-Cookie</h4>
</BODY>
</HTML>

Ðang cach su dung $HTTP_COOKIÐ_VARS do Iay gIa frI cua cookIo fruoc do frong
frang hffpcookIovar.php nhu ví du 5-10.
VI du 5-10: Su dung $HTTP_COOK¡K_VARS

<?php
echo $HTTP_COOKIE_VARS["huukhang"];
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Get Cookie</h4>
</BODY>
</HTML>


Kof qua frình bay nhu hình 5-10.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 5-10: Ðung $HTTP_COOKIÐ_VARS
Ðang cach su dung ham sossIon_gof_cookIo_params do Iay gIa frI cua cookIo fruoc do
frong frang sossIongofcookIo.php nhu ví du 5-11.
VI du 5-11: Su dung sess¡on_get_cook¡e_purums

<?php
session_start();
$myvalue= session_get_cookie_params();
echo $myvalue[1];
?>
<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Get Cookie</h4>
</BODY>
</HTML>

3. KKT L¡AN
Trong baI nay. chung fa fìm hIou cach su dung bIon sossIon va cookIo.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mon hoc: PHP

Bu¡ 6

Baøi hoïc naøy chuùng ta seõ laøm quen caùch khai baùo haøm, cheøn taäp
tin vaø taäp tin duøng chung:

9 Caùch khai baùo haøm.
9 Xaây döïng taäp tin ñònh daïng noäi dung
9 Taäp tin duøng chung


KHA¡ BAO HAM TRONG PHP
Ham do nguoI su dung dInh nghIa cho phop ban xu Iy nhung fac vu fhuong Iap dI Iap
IaI frong ung dung.
Ðo khaI bao ham. ban su dung fu khoa funcfIon voI cu phap fuong fu nhu sau:
1.

function functioname($parameter)
{
return value;
}

Trong fruong hop ham khong co gIa frI fra vo fhì ham duoc xom nhu fhu fuc. NgoaI ra.
ban co fho khaI bao fham so fuy chon bang cach gan gIa frI mac dInh cho fham so. Ví
du chung fa khaI bao:

function functioname($parameter1, $parameter2=10 )
{
return value;
}

ÐoI voI fruong hop nay fhì fham so $paramofor1 Ia fham so baf buoc va fham so
$paramofor2 Ia fham so fuy chon. khI goI ham nou khong cung cap fham so cho
$paramofor2 fhì fham so nay co gIa frI Ia 10.
Ví du. ban khaI bao frang funcfIon.php co ham gofRosuIf nhan haI so va phop foan sau
do fuy fhuoc vao phop foan ham fra vo kof qua. Nou nguoI su dung khong cung vap
phop foan fhì mac dInh Ia phop foan +.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Function</h4>
<?php
function getResult($number1, $number2,$operator="+")
{
$result=0;
switch($operator)
{
case "+":
$result=$number1+$number2;
break;
case "-":
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

$result=$number1-$number2;
break;
case "*":
$result=$number1*$number2;
break;
case "/":
if($number2!=0)
$result=$number1/$number2;
else
$result=0;
break;
case "%":
if($number2!=0)
$result=$number1%$number2;
else
$result=0;
break;
}
return $result;
}
echo "result of default operator: ".getResult(10,20);
echo "<br>";
echo "result of * operator: ".getResult(10,20,"*");
?>
</BODY>
</HTML>

Nou muon dInh nghIa funcfIon khong co gIa frI fra vo. ban co fho khaI bao frong frang
voId.php nhu ví du sau:

...
function calloperator()
{
echo "result of default operator: ".getResult(10,20);
echo "<br>";
echo "result of * operator: ".getResult(10,20,"*");
}
calloperator();
?>
</BODY>
</HTML>

Trong fruong hop fruyon fham so nhu fham bIon. ban su dung ky hIou & fruoc fham so.
chang han chung fa khaI bao ham co fham bIon co fon avorago nhu frong frang
roforonco.php nhu sau:

<HTML>
<HEAD>
<TITLE>::Welcome to PHP</TITLE>
</HEAD>
<BODY>
<h4>Function</h4>
<?php
function getAmount($quantity, $price,&$average)
{
$result=0;
$result=$quantity*$price;
$average=$result*6/12;
return $result;
}
$bq=0;
echo "result is : ".getAmount(10,20,$bq);
echo "<br>";
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

echo "result of Average is : ".$bq;
echo "<br>";
function getAmounts($quantity, $price,$average)
{
$result=0;
$result=$quantity*$price;
$average=$result*6/12;
return $result;
}
$bq=0;
echo "result is : ".getAmounts(10,20,$bq);
echo "<br>";
echo "result of Average is : ".$bq;
?>
</BODY>
</HTML>

Trong fruong hop fron fhì ham gofAmounf co fham so $avorago Ia fham bIon con ham
gofAmounfs co fham so $avorago Ia fham frI. va kof qua fra vo cua bIon $bq khI goI
ham gofAmounf Ia 100 frong khI do gIa frI cua bIon nay frong ham gofAmounfs Ia 0.
2. XAY O¡NG TAP T¡N O¡NH OANG NO¡ O¡NG
KhI frình bay noI dung fron frang HTML hay frang PHP. do fhong nhaf dInh dang
chuoI frong fho body hay fho div chang han ban can khaI bao fho style frong fho
<head>.

<style>
A {
COLOR: #003063;
TEXT-DECORATION: none
}
A:hover {
COLOR: #003063;
TEXT-DECORATION: underline
}
A:link {
FONT-WEIGHT: bold;
COLOR: red;
TEXT-DECORATION: none
}
A:visited {
FONT-WEIGHT: bold;
COLOR: black;
TEXT-DECORATION: none
}
.title {
FONT-WEIGHT: normal;
FONT-SIZE: 22px
}
.text{
FONT: 11px Arial, Helvetica, sans-serif
}
</style>

Trong do. A fuong ung voI IIon kof |chuoI frong fho <a>) co dInh dang ung voI fruong
hop IIon kof. dI chuyon con chuof. chon IIon kof.

A {
COLOR: #003063;
TEXT-DECORATION: none
}
A:hover {
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

COLOR: #003063;
TEXT-DECORATION: underline
}
A:link {
FONT-WEIGHT: bold;
COLOR: red;
TEXT-DECORATION: none
}
A:visited {
FONT-WEIGHT: bold;
COLOR: black;
TEXT-DECORATION: none
}

Chang han. chung fa khaI bao frang PHP voI noI dung duoc ap dung voI kIou dInh dang
khaI bao frong fho style nhu vu du 6-1.
VI du 6-1: Khu¡ buo the style

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Style trong PHP</title>
<style>
A {
COLOR: #003063;
TEXT-DECORATION: none
}
A:hover {
COLOR: #003063;
TEXT-DECORATION: underline
}
A:link {
FONT-WEIGHT: bold;
COLOR: red;
TEXT-DECORATION: none
}
A:visited {
FONT-WEIGHT: bold;
COLOR: black;
TEXT-DECORATION: none
}
.title {
FONT-WEIGHT: normal;
FONT-SIZE: 22px;
COLOR: #003063;
}
.text{
FONT: 11px Arial, Helvetica, sans-serif
}
</style>
</head>
<body>
<h4>Style Tag</h4>
<TABLE cellSpacing=0 cellPadding=0
width="100%" border=0>
<TR>
<TD vAlign=top class=title>
*** Quaûn Trò SQL Server 2000 *** </TD>
</TR>
<TR>
<TD class=text>
<div align=justify>
Tìm hieåu caùch caøi ñaët, caáu hình, quaûn trò,
backup & restore, import & export, thieát
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

keá, laäp trình, töï ñoäng hoaù taùc vuï quaûn trò,
baûn sao döõ lieäu, baûo maät vaø choáng thaâm nhaäp
döõ lieäu baèng.
<b>SQL Injection</b>.</div>
</TD>
</TR>
<TR><TD><hr size=1 color=red></TD</TR>
<TR><TD>Welcome to
<a href="www.huukhang.com" class=>
www.huukhang.com</a></TD
</TR>
</TABLE>
</body>
</html>

KhI frIou goI frang style.PHP fron frình duyof. noI dung cua frang web duoc dInh dang
fhoo fho style nhu hình 6-1.


Hình 6-1: Ap dung fho sfyIo
Tuong fu nhu vay khI ban muon fhong nhaf noI dung frong nhung fho khac cua mof
frang web fhì khaI bao mof dInh dang frong fho style. Tuy nhIon. khI daf fon frung voI
fho HTML. moI fho do frong frang so cung chung mof dInh dang. Chang han. ban khaI
bao dInh dang cho fho td nhu sau:

TD {
FONT: 10px Arial, Helvetica, sans-serif
}

MoI noI dung frình bay frong fho td so co dInh dang nhu fron. Nou ban muon co dInh
dang khac fhì khaI bao fhuoc fính class cho fho td do. ví du su dung dInh dang khac
cho fho td:

<td class=text>ABC</td>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Thay vì chuoI ABC so co dInh dang Ia FONT: 10px Arial. Helvetica, sans-serif fhì chung
so co dInh dang cua FONT: 11px Arial, Helvetica, sans-serif.
Chu y rang. frong moI frang web ban phaI khaI bao fho style va dInh nghIa fhong nhaf
cho cac fho. KhI co su fhay doI ban phaI fhay doI frong moI frang web. Ðo su dung
chung cho moI frang web frong ung dung. ban can xay dung mof fap fIn style. fap fIn
duoc bIof don voI fon goI custom style sheet |css).
Ðaf ky frang web nao frong ung dung. muon ap dung kIou dInh dang frong fap fIn css
fhì khaI bao IIon kof fap fIn css bang fho link.
Ví du. chung fa khaI bao fap fIn style.css bao gom cac dInh dang nhu ví du 6-2.
VI du 6-2: Khu¡ buo tup t¡n css

A {
COLOR: #003063;
TEXT-DECORATION: none
}
A:hover {
COLOR: #003063;
TEXT-DECORATION: underline
}
A:link {
FONT-WEIGHT: bold;
COLOR: red;
TEXT-DECORATION: none
}
A:visited {
FONT-WEIGHT: bold;
COLOR: black;
TEXT-DECORATION: none
}
.title {
FONT-WEIGHT: bold;
FONT-SIZE: 14px;
COLOR: #003063;
}
.text{
FONT: 11px Arial, Helvetica, sans-serif
}

Sau do frong frang PHP. ban khaI bao IIon kof fap fIn nay bang fho link. nou muon ap
dung dInh dang nay frong moI fho HTML ban su dung fhuoc fính class nhu khaI bao
dInh dang cua fho style ngay frong frang do nhu ví du 6-3.
VI du 6-3: Khu¡ buo su dung tup t¡n css

<html>
<head>
<title>
Welcome to Link Style Sheet File
</title>
<LINK href="style.css" rel=stylesheet>
<META http-equiv=Content-Type
content="text/html; charset=utf-8">
</head>
<body>
<h4>Style File</h4>
<TABLE cellSpacing=0 cellPadding=0
width="100%" border=0>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<TR>
<TD vAlign=top class=title>
*** Quaûn Trò SQL Server 2000 *** </TD>
</TR>
<TR>
<TD class=text>
<div align=justify>
Tìm hieåu caùch caøi ñaët, caáu hình, quaûn trò,
backup & restore, import & export, thieát
keá, laäp trình, töï ñoäng hoaù taùc vuï quaûn trò,
baûn sao döõ lieäu, baûo maät vaø choáng thaâm nhaäp
döõ lieäu baèng.
<b>SQL Injection</b>.</div>
</TD>
</TR>
<TR><TD><hr size=1 color=red></TD</TR>
<TR><TD>Welcome to
<a href="www.huukhang.com" class=>
www.huukhang.com</a></TD
</TR>
</TABLE>
</body>
</html>

TrIou goI frang includestyle.php fron frình duyof nhu hình 6-3. mau va kích fhuoc font
cung voI kIou chua cua noI dung khong fhay doI so voI style.php. boI vì phan fho style
duoc fach ra fhanh fap fIn style.css. sau do dung fho link do IIon kof fap fIn css nay vao
frang PHP fro IaI.



Hình 6-3: IIon kof fap fIn css
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Chu y rang. nou khaI bao fhuoc fính cIass frong fho <table> fhì nhung noI dung frong
fho <table> so co dInh dang theo dInh dang khaI bao frong fhuoc fính class. Tuong fu.
nou khaI bao fhuoc fính class frong fho <tr> fhì noI dung frong fho <fr> so co dInh
dang gIong nhu dInh dang khaI bao frong fhong fIn class.
3. THONG NHAT KÍCH TH¡OC C¡A MO¡ TRANG PHP
KhI xay dung ung dung web chuyon nghIop. dIou dau fIon ban non quan fam Ia su
fhong nhaf vo kích fhuoc cua cac phan fron frang web. ÐIou nay co nghIa Ia khI nguoI
su dung fhay doI frang web khI duyof. phan top, left, right, bottom co kích fhuoc nhu
nhau.
Ðo Iam dIou nay. ban chIa frang web ra fhanh 5 phan: top. left. right, body va bottom.
Phan fop fhuong frình bay cac fhuoc fính nhu quan cao |baner). logo |bIou fuong cua
cong fy). menu |fhuc don cua ung dung) va mof so fhong fIn khac.
Phan left Ia fhong fIn vo cac menu phu hay con goI Ia menu cua menu chính. bon canh
menu con nay frang web fhuong co cac IIon kof vo IIon ho. quang cao. mailing list
|dang ky email). goI don ban bo |send to friend). ....
ÐoI voI phan right. fhuong Ia phan gIoI fhIou vo cac fhong dac bIof va quang cao.
chang han doI voI ung dung ban sach. phan right fhuong Ia danh sach cac nhom sach
ban chay. sap phaf hanh. ...
Phan bottom fhuong fhong fIn IIon Iac cua cong fy. chu nhan cua web site va ban
quyon. NgoaI ra. phan bottom doI khI Ia danh sach cac menu con khac.
Tom IaI. fuy fhuoc vao y fuong fhIof ko moI phan nhu fron bao gom cac fhuoc fính ma
nha fhIof ko can frình bay sao cho phu hop. Tuy nhIon. phan body Ia phan frình bay
noI dung chính cua moI frang web. NgoaI ra. fuy vao fung fruong hop cu fho. frang web
co fho khong co phan left va right.
Nhu vay. chung fa so chIa frang web ra fhanh 5 phan. phan body chính Ia phan chính
cua frang web do. con 4 phan con IaI duoc chon vao khI co nhu cau.
Chang han. co nhung frang web do fhong fIn frình bay frong phan body nhIou. non can
khong gIa Ion hon. ban co fho khong can su dung haI phan left va right.
Ðo Iam dIou nay. fruoc fIon chung fa fhIof ko frang sample.php co 5 phan nhu hình 6-3.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 6-3: Trang sampIo.php
-------------------------------------------------------------------------------------------
Löu yù:
Taïo moät table goàm 3 haøng 3 coät vaø khai baùo border=1 ñeå ñeã canh leà sau ñoù baïn coù theå
khai baùo laïi thuoäc tính naøy baèng 0.





Phaàn top vaø bottom laø moät haøng vaø merge 3 coät thaønh 1.
Beân trong moãi phaàn coù theå coù moät hay nhieàu theû table khaùc.
Coù theå khoâng coù phaàn left vaø right nhöng baét buoäc phaàn top vaø bottom phaûi coù.
Baïn coù theå söû duïng chieàu roäng cuûa table theo kích thöôùc töông ñoái (%) hay soá chæ ñònh,
ñoái vôùi maøn hình 600*800 thì chieàu roäng thöôøng söû duïng laø 780, khi ngöôøi söû duïng choïn
ñoä phaân giaûi cuûa maøn hình lôùn hôn thì kích thöôùc cuûa table naøy khoâng thay ñoåi, trong
khi ñoù noäi dung seõ phuû ñaày maøn hình khi baïn khai baùo kích thöôùc theo 100%.
-------------------------------------------------------------------------------------------
Ðo co gIao dIon nhu frang sample.php nhu fron. ban co fho khaI bao nhu ví du 6-3.
VI du 6-3: No¡ dung trung sumple.PHP

<html>
<head>
<title>
Welcome to Including File
</title>
<LINK href="style.css" rel=stylesheet>
<META http-equiv=Content-Type
content="text/html; charset=utf-8">
</head>
<body bottomMargin=0 leftMargin=0
topMargin=0 rightMargin=0>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<TABLE cellSpacing=2 cellPadding=2
width="778" border=1 align=center>
<TR HEIGHT="100">
<TD Align=center colspan=3>
TOP
</TD>
</TR>
<TR HEIGHT="280">
<TD vAlign=top width="20%">
LEFT
</TD>
<TD vAlign=top width="60%">
BODY
</TD>
<TD vAlign=top width="20%">
RIGHT
</TD>
</TR>
<TR HEIGHT="50">
<TD colspan=3 align=center>
BOTTOM
</TD
</TR>
</TABLE>
</body>
</html>

Trong fruong hop ban muon co duong phan cach gIua moI phan bang image. ban co fho
khaI bao IaI frang sample.php co 5 hang va 5 cof nhu template.php nhu hình 6-4.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 2-4: Phan cach co vIong
Ðo frình bay frang tempale.PHP nhu hình 6-4. ban khaI bao noI dung frang nay nhu ví
du 6-4.
VI du 6-4: Khu¡ buo templute.php

<html>
<head>
<title>
Welcome to Including File
</title>
<LINK href="style.css" rel=stylesheet>
<META http-equiv=Content-Type
content="text/html; charset=utf-8">
</head>
<body bottomMargin=0 leftMargin=0
topMargin=0 rightMargin=0>
<TABLE width="778" border=0 cellSpacing=0
cellPadding=0 align=center>
<TR HEIGHT="100">
<TD Align=center colspan=5>
TOP
</TD>
</TR>
<!---Khai baùo ñöôøng phaân caùch--->
<TR HEIGHT="1">
<TD colspan=5 bgcolor=gray></TD>
</TR>
<TR HEIGHT="280">
<TD vAlign=top width="150">LEFT</TD>
<!---Khai baùo ñöôøng phaân caùch--->
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<TD bgcolor=gray width="1"></TD>
<TD vAlign=top width="476">BODY</TD>
<!---Khai baùo ñöôøng phaân caùch--->
<TD bgcolor=gray width="1"></TD>
<TD vAlign=top width="150">RIGHT</TD>
</TR>
<!---Khai baùo ñöôøng phaân caùch--->
<TR HEIGHT="1">
<TD colspan=5 bgcolor=gray></TD>
</TR>
<TR HEIGHT="50">
<TD colspan=5 align=center>
BOTTOM
</TD
</TR>
</TABLE>
</body>
</html>

Sau do fach frang template.php nay fhanh 5 frang khac nhau duoc daf fon fuong ung Ia
top.htm, left.htm, right.htm va bottom.htm. frong do phan body fuong ung voI frang
templates.php.
Ðo khaI bao chon fap fIn frong frang PHP. ban su dung cu phap nhu sau:

<?php
include(”filename”);
?>

Hay

<?php
require(”filename”);
?>
Trong do frang templates.PHP khaI bao chon top.htm, left.htm, right.htm va bottom.htm
nhu ví du 6-5.
VI du 6-5: Khu¡ buo chen tup t¡n trong templutes.php

<html>
<head>
<title>
Welcome to HUUKHANG.COM
</title>
<LINK href="style.css" rel=stylesheet>
<META http-equiv=Content-Type
content="text/html; charset=utf-8">
</head>
<body bottomMargin=0 leftMargin=0
topMargin=0 rightMargin=0>
<TABLE width="778" border=0 cellSpacing=0
cellPadding=0 align=center>
<TR HEIGHT="100">
<TD Align=center colspan=5>
<?php include("top.htm")?>
</TD>
</TR>
<!---Khai baùo ñöôøng phaân caùch--->
<TR HEIGHT="1">
<TD colspan=5 bgcolor=gray></TD>
</TR>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<TR HEIGHT="280">
<TD vAlign=top width="150">
<?php include(“left.htm")?>
</TD>
<!---Khai baùo ñöôøng phaân caùch--->
<TD bgcolor=gray width="1"></TD>
<TD vAlign=top width="476">BODY</TD>
<!---Khai baùo ñöôøng phaân caùch--->
<TD bgcolor=gray width="1"></TD>
<TD vAlign=top width="150">
<?php include ("right.htm")?>
</TD>
</TR>
<!---Khai baùo ñöôøng phaân caùch--->
<TR HEIGHT="1">
<TD colspan=5 bgcolor=gray></TD>
</TR>
<TR HEIGHT="50">
<TD colspan=5 align=center>
<?php include("bottom.htm")?>
</TD
</TR>
</TABLE>
</body>
</html>

KhI frIou goI frang templates.php. noI dung cua 4 fang left.htm. right.htm, top.htm,
bottom.htm chon vao frang templates.php nhu hình 6-5.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 6-5: Trang fompIafos.php sau khI chon
Trong do. noI dung cua frang top.htm dInh nghIa fuong fu nhu ví du 6-5-1.
VI du 6-5-1: No¡ dung trung top.htm

<TABLE width="100%" border=0 cellSpacing=0
cellPadding=0 HEIGHT="100%" align=center>
<TR >
<TD width="150" Align=center>
LOGO
</TD>
<TD Align=center>
BANNER
</TD>
</TR>
<TR HEIGHT="1">
<TD colspan=2 bgcolor=gray></TD>
</TR>
<TR HEIGHT="20%" bgcolor=black class=menu>
<TD width="150" >
Welcome
</TD>
<TD>
Home | Search | Contact | Help
</TD>
</TR>
</TABLE>

NoI dung cua fap fIn left.htm duoc dInh nghIa fuong fu nhu ví du 6-5-2.
VI du 6-5-2: No¡ dung trung leIt.htm

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<TABLE width="100%" border=0 cellSpacing=0
cellPadding=0 HEIGHT="100%" align=center>
<TR >
<TD width="150" Align=center>
LEFT
</TD>
</TR>
</TABLE>

Nou co su dung frang rIghf.hfm fhì noI dung cua fap fIn nay duoc dInh nghIa fuong fu
nhu ví du 6-5-3.
VI du 6-5-3: No¡ dung trung r¡ght.htm

<TABLE width="100%" border=0 cellSpacing=0
cellPadding=0 HEIGHT="100%" align=center>
<TR >
<TD width="150" Align=center>
FREE ADV
</TD>
</TR>
</TABLE>

Tuong fu nhu vay. frang bottom.htm co noI dung nhu ví du 6-5-4.
VI du 6-5-4: No¡ dung trung bottom.htm

<TABLE width="100%" border=0 cellSpacing=0
cellPadding=0 HEIGHT="100%" align=center>
<TR class=text>
<TD Align=center>
HUUKHANG.COM<br>
Copyright ©2000-2005.
All Rights Reserved.
</TD>
</TR>
</TABLE>

Chu y rang. frong moI frang khaI bao chon khong co cac fho dong va mo html. body boI
khI chon fhì noI dung cua fap fIn duoc chon so duoc chon vao fap fIn bI chon va frong
fap fIn bI chon da co haI fho nay.
KIch ban frình chu PHP ho fro cac fap fIn duoc chon voI cac fon mo rong nhu htm,
PHP, inc, lib, html. Ðo fhuc chaf cua vIoc khaI bao chon Ia chon doan ma frong fap fIn
chon vao fap fIn bo chon. frong fruong hop nay frang chon htm hay PHP dou gIong
nhau do Ia Iy do faI sao cac frang chon o fron dou co fon mo rong Ia htm.
Tuy nhIon. khI ban goI frang chon nay mof mình ví du tom.htm. nou bon frong co ma
PHP fhì ma do khong duoc fhong dIch. Nou nhung frang chon nay co nhu cau goI mof
mình fhì ban co fho chuyon chung fhanh frang PHP fhay vì htm nhu da frình bay.
Sau khI co duoc frang templates.php. ban co fho su dung frang nay Ia mau cho cac
frang khac bang cach save as fhanh cac frang PHP khac khI Iap frình. KhI khaI bao
chon fap fIn. ban co fho su dung duong dan fuong doI hoac fuyof doI cua fap fIn chon
so voI ap fIn bI chon.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

4. TAP T¡N O¡NG CH¡NG
NgoaI cach chon o fron. nou ban co nhung ham su dung chung cho cac frang PHP khac
fhì ban khaI bao fhanh mof frang PHP khac sau do dung cu phap chon fap fIn do chon
chung vao khI co nhu cau.
Ví du frong fruong hop nay chung fa muon su dung chung ham co fon gofPagIng nhan 5
fham so $fofaIRovs |fong so mau fIn). $curPg |so frang hIon hanh). $pg |so frang frình
bay). $ro |so mau fIn fron 1 frang). $fIIo |frang php can goI) frong fap fIn pagIng.php.

<?php
function paging($totalRows,$curPg,$pg,$re,$file)
{
$paging="";
$mxR = $re;
$mxP = $pg;
if($totalRows%$mxR==0)
$totalPages = (int)($totalRows/$mxR);
else
$totalPages = (int)($totalRows/$mxR+1);
$curRow = ($curPg-1)*$mxR+1;
if($totalRows>$mxR)
{
$start=1;
$end=1;
$paging1 ="";
for($i=1;$i<=$totalPages;$i++)
{
if(($i>((int)(($curPg-1)/$mxP))* $mxP) && ($i<=((int)(($curPg-
1)/$mxP+1))* $mxP))
{
if($start==1) $start=$i;
if($i==$curPg)
$paging1 .= $i."&nbsp;&nbsp;";
else
{
$paging1 .= "<a class=lslink href='$file";
$paging1 .="&page=".$i."'>".$i;
$paging1 .="</a>&nbsp;&nbsp;";
}
$end=$i;
}
}
$paging.= "Trang :&nbsp;&nbsp;" ;
if($curPg>$mxP)
{
$paging .="<a class=lslink href='$file";
$paging .="&page=".($start-1);
$paging .="'>Previous</a>&nbsp;&nbsp;";
}
$paging.=$paging1;
if(((($curPg-1)/$mxP+1)*$mxP) < $totalPages)
{
$paging .= "<a class=lslink href='$file";
$paging .="&page=".($end+1);
$paging .="'>Next</a>&nbsp;&nbsp;";
}
}
return $paging;
}
?>



CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Sau do khaI bao frang rosuIf.php. chon fap fIn pagIng.php va goI ham gofPagIng nhuu
sau:

<html>
<head>
<title>
Welcome to HUUKHANG.COM
</title>
<LINK href="style.css" rel=stylesheet>
<META http-equiv=Content-Type
content="text/html; charset=utf-8">
</head>
<body bottomMargin=0 leftMargin=0 topMargin=0 rightMargin=0>
<?php
include("paging.php");
echo paging(47,2,10,5,"result.php?x=10");
?>
</body>
</html>


Kof qua fra vo nhu hình 6-6 sau




Hình 6-6: Ham dung chung
5. KKT L¡AN
Trong baI nay. chung fa fìm hIou cach khaI bao ham. frang php va khaI bao chon fap
fIn.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mon hoc: PHP

Bu¡ ?

Baøi hoïc naøy chuùng ta seõ laøm quen caùch xöû lyù chuoãi, maûng, kieåu DataTime
trong PHP:

9 Xöû lyù chuoãi
9 Laøm vieäc vôùi maûng döõ lieäu
9 Kieåu DateTime


X¡ LY CH¡O¡
PHP Ia kIch ban duoc xom Ia fof nhaf cho xu Iy chuoI. bang cach su dung cac ham xu Iy chuoI. ban
co fho fhuc hIon cac y dInh cua mình khI fuong fac co so du IIou. fap fIn hay du IIou khac.
1.
1.1. Ojnh dung chuo¡
KhI xuaf kof qua ra frình duyof. ban co fho su dung cac dInh dang chuoI fuong fu nhu ngon ngu
Iap frình C. Chang han. chung fa In gIa frI cua bIon $I frong frang dInhdang.php nhu ví du 7-1.

<Dt.ì>
<D-¬c>
<titì->:triDo FoD·ticD:<titì->
<D-¬c>
<rccv>
<D4>LiDD c¬Do<D4>
<.rDr
:iì2.cc;
:¸ìDD;
-·Dc `Tct¬ì ¬.coDt c1 crc-r: :i<rr>`;
rriDt1(`Tct¬ì ¬.coDt c1 crc-r: °.ì1`, :i);
-·Dc `<rr>`;
rriDt1(`Tct¬ì ¬.coDt c1 crc-r: °.21`, :i);
-·Dc `<rr>`;
rriDt1(`Tct¬ì ¬.coDt c1 crc-r: i°.21, ¸°.D1`, :i,:¸);
.>
<rccv>
<Dt.ì>

Kof qua xuaf hIon nhu hình 7-1

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 7-1: ÐInh dang chuoI In
Trong do cac dInh dang duoc chIa ra nhIou IoaI fuy fhuoc vao cac ky fu ban su dung.

° ÷ KcDo v-o ·¬o tD¬. :c.
r – TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do riD¬rv.
· ÷ TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do .¬ A:Cíí.
c ÷ TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do c-·i.¬ì.
- ÷ TriDD r¬v c¬Do :c ìcoi· ¬ Di-D tDd· cdci c¬Do ì.2-+2.
o ÷ TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do c-·i.¬ì ×DcDo c¬o.
1 ÷ TriDD r¬v c¬Do :c 1ìc¬t ¬ Di-D tDd· cdci c¬Do :c ·D¬. ¹cDo.
c ÷ TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do D- :c ìD.
: ÷ TriDD r¬v c¬Do ·Doci.
× ÷ TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do D- :c ìt ci ×v td tDdcDo.
X ÷ TriDD r¬v c¬Do :c iDt-o-r ¬ Di-D tDd· cdci c¬Do D- :c ìt ci ×v td Dc¬.
1.2. Hum chuyen do¡ chuo¡
Ðo chuyon doI chuoI ra ky fu hoa fhuong ban su dung mof frong 4 ham nhu ví du 7-2 frong frang
chuyondoI.php:

<Dt.ì>
<D-¬c>
<titì->:triDo FoD·ticD:<titì->
<D-¬c>
<rccv>
<D4>CDov-D cci<D4>
<.rDr
::tr`'-ì·c.- tc Doo×D¬Do.·c.`;
-·Dc ::tr;
-·Dc `<rr>`;
-·Dc :trtcorr-r(::tr);
-·Dc `<rr>`;
-·Dc :trtcìcw-r(::tr);
-·Dc `<rr>`;
-·Dc o·1ir:t(::tr);
-·Dc `<rr>`;
-·Dc o·wcrc:(::tr);
-·Dc `<rr>`;
.>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<rccv>
<Dt.ì>


Kof qua frình bay nhu hình 7-2.



Hình 7-2: Chuyon doI chuoI
1.3. Hum tuch huy ket hop chuo¡
Ðo fach hay kof hop chuoI. ban su dung mof frong cac ham fhuong su dung nhu sfrfok. oxpIodo
hay subsfr. Chang han. chung fa su dung 4 ham nay frong ví du 7-4 frong frang fachchuoI.php.

<Dt.ì>
<D-¬c>
<titì->:triDo FoD·ticD:<titì->
<D-¬c>
<rccv>
<D4>T¬·D Dcr ·Doci<D4>
<.rDr
::triDo `XiD ·D¬c r¬D c¬ c-D ci Doo×D¬Do.·c.`;
::tr ::triDo;
-·Dc ::triDo.`<rr>`;
:tc× :trtc×(::triDo, ` `);
wDiì- (:tc×)
¹
-·Dc `'crc :tc×<rr >`;
:tc× :trtc×(` `D`t`);
·
-·Dc ::tr.`<rr>`;
-·Dc :or:tr(::tr,24).`<rr>`;
:¬`¬rr¬v();
:¬-×rìcc-(` `,::tr);
wDiì-(:i-¬·D(:¬))
¹
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

-·Dc :i`¬ìo-``.`<rr>`;
·
.>
<rccv>
<Dt.ì>

Kof qua frình bay nhu hình 7-4.



Hình 7-4: Ham fach chuoI
Trong fruong hop kof hop gIa frI cua cac phan fu cua mang fhanh chuoI. ban su dung ham ImpIodo
nhu ví du 7-5 frong frang kofhop.php:

<Dt.ì>
<D-¬c>
<titì->:triDo FoD·ticD:<titì->
<D-¬c>
<rccv>
<D4>K-t Dcr ·Doci<D4>
<.rDr
::tr `XiD ·D¬c r¬D c¬ c-D ci Doo×D¬Do.·c.`;
:¬`¬rr¬v();
:¬-×rìcc-(` `,::tr);
wDiì-(:i-¬·D(:¬))
¹
-·Dc :i`¬ìo-``.`<rr>`;
·
::tri.rìcc-(` `,:¬);
-·Dc ::tr;
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

.>
<rccv>
<Dt.ì>


Kof qua frình bay nhu hình 7-5.



Hình 7-5: Ham kof hop chuoI
1.4. TIm k¡em vu thuy the chuo¡
Ðo fhay fho chuoI. ban su dung ham sfr_ropIaco. chang han frong fruong hop hop ban Iay gIa frI
fu fho nhap IIou. sau do fìm kIom nou phaf hIon dau ‘ fhì fhay fho fhanh haI dau nhay nhu frang
ropIaco.php.

<Dt.ì>
<D-¬c>
<titì->:triDo FoD·ticD:<titì->
<D-¬c>
<rccv>
<D4>TD¬t tD- ·Doci<D4>
<.rDr
::tr``;
i1 (i::-t(:t×t1oììD¬.-))
::tr :t×t1oììD¬.-;
i1(::tr . ``);
::tr:tr_r-r쬷-(`c`,`¬`,::tr);
-·Dc ::tr.`<rr>`;
.>
<1cr. ¬·ticDr-r쬷-.rDr .-tDccrc:t>
1oììD¬.-: <iDrot D¬.-t×t1oììD¬.- ¬ìo-`<.::tr.>`><rr>
<iDrot tvr-:or.it “o-:or.it>
<1cr.>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

<rccv>
<Dt.ì>

KhI frIou goI frang ropIaco.php fron frình duyof. ban so co kof qua nhu sau:



Hình 7-6: Ham fhay fho chuoI
NgoaI ra. ban co fho su dung cac ham nhu sfrpos |fra vo vI frí chuoI con frong chuoI mo). ...
2. LAM V¡KC VO¡ MANG O¡ L¡K¡
Nhu frong baI kIou du IIou chung fa da Iam quon voI kIou du IIou mang. frong phan nay chung fa
fIop fuc fìm hIou cac khaI bao. fruy cap va fuong fac voI fap fIn fu mang mof chIou. haI chIou.
2.1. Mung mot ch¡eu
Ðo khaI bao mang mof chIou. ban co fho su dung cu phap nhu sau:

:¬rr¬rr¬v();
:¬rr:¬rr¬v(c);

Truy cap vao phan fu mang. ban co fho su dung chI muc cua phan fu nhu sau:

:¬rrD`ì;
:¬rr:ì`ì2;

Iay gIa frI cua phan fu mang. ban cung fhuc hIon fuong fu nhu fruong hop fruy cap mang phan
fu.

-·Dc :¬rrD`;
:×:¬rr:c`;

Chang han. chung fa khaI bao mang dong va mang co so phan fu cho fruoc. sau do fruy cap va
Iay gIa frI cua chung nhu ví du frong frang arrayono.php sau:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


<Dt.ì>
<D-¬c>
<titì->Arr¬v<titì->
<D-¬c>
<rccv>
<D4>\¬Do .ct ·Di-o<D4>
<.rDr
:iD;
:.v¬rr¬rr¬v(ì,2,·,4,c,t,¯);
:¬rr¬rr¬v();
:¬rr:¬rr¬v(ìD);
:¬rrD`ìD;:¬rrì`ìì;:¬rr2`ì2;:¬rr·`ì·;
1cr(:iD;:i<:i:-c1(:¬rr);:i++)
¹
-·Dc :¬rr:i`.` `;
·
-·Dc `<rr>`;
-·Dc `Ci¬ tri ìcD DD¬t `..¬×(:¬rr).`<rr>`;
-·Dc `Ci¬ tri DDc DD¬t `..iD(:¬rr).`<rr>` ;
-·Dc `Ci¬ tri troDo riDD `.¬rr¬v_:o.(:¬rr) :i:-c1(:¬rr).`<rr>` ;
-·Dc `<rr>`;
1cr(:iD;:i<ìD;:i++)
¹
:¬rr::i`ìD+:i;
·
1cr(:iD;:i<ìD;:i++)
¹
-·Dc :¬rr::i`.` `;
·
-·Dc `<rr>`;
-·Dc `Ci¬ tri ìcD DD¬t `..¬×(:¬rr:).`<rr>`;
-·Dc `Ci¬ tri DDc DD¬t `..iD(:¬rr:).`<rr>` ;
-·Dc `Ci¬ tri troDo riDD `.¬rr¬v_:o.(:¬rr:) :i:-c1(:¬rr:).`<rr>` ;
.>
<rccv>
<Dt.ì>


Kof qua frình bay nhu hình 7-7 khI frIou goI frang arrayono.php.


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 7-7: KhaI bao va su dung mang mof chIou
2.2. Mung hu¡ ch¡eu
Tuong fu nhu mang mof chIou. frong fruong hop Iam vIoc mang haI chIou ban khaI bao fuong fu
nhu frang arrayfvo.php.

<Dt.ì>
<D-¬c>
<titì->Arr¬v<titì->
<D-¬c>
<rccv>
<D4>\¬Do D¬i ·Di-o<D4>
<.rDr
:iD;:¸D;
:¬rr¬rr¬v();
:¬rrD`D`ìD;
:¬rrD`ì`ìì;
:¬rrD`2`ì2;
:¬rrì`D`ì·;
:¬rrì`ì`ì4;
:¬rrì`2`ìc;
:¬rr2`D`ìt;
:¬rr2`ì`ì¯;
:¬rr2`2`ìº;
1cr(:iD;:i<:i:-c1(:¬rr);:i++)
¹
1cr(:¸D;:¸<:i:-c1(:¬rr);:¸++)
¹
-·Dc :¬rr:i`:¸`.` `;
·
-·Dc `<rr>`;
·
-·Dc `<rr>`;

:¬rr:¬rr¬v(¬rr¬v(ì,2,·,4,c,t,¯),
¬rr¬v(ìì,ì2,ì·,ì4,ìc,ìt,ì¯));
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

1cr(:iD;:i<¯;:i++)
¹
1cr(:¸D;:¸<¯;:¸++)
¹
:¬rr::i`:¸`ìD+:i*:¸;
·
·

1cr(:iD;:i<¯;:i++)
¹
1cr(:¸D;:¸<¯;:¸++)
¹
-·Dc :¬rr::i`:¸`.` `;
·
-·Dc `<rr>`;
·
-·Dc `<rr>`;

.>
<rccv>
<Dt.ì>

KhI frIou goI frang nay fron frình duyof. kof qua frình bay nhu hình 7-8.




Hình 7-8: Mang haI chIou
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

3. K¡K¡ OATKT¡MK
Ðo Iam vIoc voI kIou du IIou Ðafo va TImo. ban su dung ham cua PHP co san. Chang han. muon
frình bay chuoI ngay fhang. ban dung ham dafo voI cac fham so nhu ví du sau:

<Dt.ì>
<D-¬c>
<titì->L¬t- ¬Dc Ti.-<titì->
<D-¬c>
<rccv>
<D4>No¬v Di-D t¬i<D4>
<.rDr
-·Dc c¬t-(`¸÷:÷F÷Y`);
-·Dc `<rr>`;
-·Dc c¬t-(`\Y`);
-·Dc `<rr>`;
-·Dc `L¬v: c1 `.c¬t-(`\`).` i: `.c¬t-(`t`);
-·Dc `<rr>`;
.>

<rccv>
<Dt.ì>

Kof qua fra vo nhu hình 7-9.



Hình 7-9: Su dung ham Ðafo

Iuu y rang. fhan so frong ham dafo duoc frình bay frong bang sau

÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Ccc- Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
¬ Foci :¬DoCDi-o r¬Do D¬i ×v td tDdcDo am/pm.
A Foci :¬DoCDi-o r¬Do D¬i ×v td Dc¬ AM/PM.
F ÐiDD c¬Do tDci oi¬D Swatch Internet, r¬D ·c tD- tD¬. ×D¬c
http://swatch.com/internettime/internettime.php3.
c Day (Dì÷·ì) trcDo tD¬Do ci D¬i :c, D-o Do¬v ì÷' :- ·c ×-. :c D.
L Day (Mon÷Sun) trcDo to¬D ci · ×v td.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

F TD¬Do (January÷December) trcDo D¬. ci t-D tD¬Do ¹¬v ¹o c¬Do t-×t.
o Hour (ì÷ì2) trcDo Do¬v ì Dc¬· 2 :c (×DcDo ×-. D D-o oic td ì÷').
C Hour (D÷2·) trcDo Do¬v ì Dc¬· 2 :c (×DcDo ×-. D D-o oic td D÷').
D Hour (Dì÷ì2) trcDo Do¬v 2 :c (×-. D D-o oic td Dì÷D').
I Hour (DD÷2·) trcDo Do¬v 2 :c (×-. DD D-o oic td DD÷D').
i Minutes (Dì÷c') ¹¬ trci co¬ (×-. DD D-o rDot td DD÷c').
¸ Day (ì÷·ì) ì Dc¬· 2 :c (×DcDo ×-. D D-o Do¬v td ì÷').
ì Day (Monday÷Sunday) trcDo to¬D c¬Do text.
L N¬. DDo¬D tr¬ - ì, Dodc· ì¬i D¬. tr¬ - D.
. Month (Dì÷ì2) trcDo D¬. 2 :c (×-. DD D-o tD¬Do td Dì÷D').
\ Month (Jan÷Dec) trcDo D¬. · ×v td.
D Month (ì÷ì2) ì Dc¬· 2 :c (×DcDo ×-. D D-o tD¬Do td ì÷').
: Seconds (Dì÷c') ¹¬ trci co¬ (×-. DD D-o oi¬v td DD÷c').
: TD-. D¬i ×v td st, nd, rd D¬v th tD-c :¬o Do¬v c¬Do D¬i ×v td :c
(i co DDd ì2
tD
).
t Tr¬ - tcDo :c Do¬v trcDo tD¬Do (td 2º ÷·ì).
T Kv td Timezone ·o¬ :-r-r ci · ×v td, ·D¬Do D¬D DDd EST.
L TcDo :c Seconds td ì .¬Do¬rv ì'¯D tci Dc. D¬v dDo ci UNIX Time Stamp.
w L¬v (D÷t) ·o¬ to¬D, D dDo ci Sunday ¬ t dDo ci Saturday.
v N¬. ¹iDD c¬Do 2 ·cD :c (D·).
Y N¬. ¹iDD c¬Do 4 ·cD :c (2DD·).
: No¬v trcDo D¬. .ct Dc¬· 2 ·cD :c (D÷·tc).
X Timezone Di-D t¬i tiDD r¬Do oi¬v td –4·2DD ¹-D 4·2DD.
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

4. KKT L¡AT
Trong baI nay. chung fa fap frung fìm hIou xu Iy chuoI. mang va ham ngay fhang. Trong baI fIop.
chung fa fIop fuc fìm hIou co so du IIou mySQI.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mon hoc: MySQL

Bu¡ 8

Baøi hoïc naøy chuùng ta seõ laøm quen caùch thao taùc treân cô sôû döõ lieäu MySQL:

9 Giôùi thieäu cô sôû döõ lieäu MySQL
9 Caøi ñaët MySQL
9 Caáu hình
9 Kieåu döõ lieäu
9 Khai baùo caùc phaùt bieåu


1. G¡O¡ TH¡K¡ CO SO O¡ L¡K¡ MYSQL
MySQL Ia co so du IIou duoc su dung cho cac ung dung Web co quy mo vua va nho. Tuy khong
phaI Ia mof co so du IIou Ion nhung chung cung co frình gIao dIon fron Windows hay Linux. cho phop
nguoI dung co fho fhao fac cac hanh dong IIon quan don co so du IIou.
Cung gIong nhu cac co so du IIou. khI Iam vIoc voI co so du IIou MySQL. ban dang ky kof noI.
fao co so du IIou. quan Iy nguoI dung. phan quyon su dung. fhIof ko doI fuong Table cua co so du IIou
va xu Iy du IIou.
Tuy nhIon. frong baf ky ung dung co so du IIou nao cung vay. nou ban fhan chung co ho fro mof
frình gIao dIon do hoa. ban co fho su dung chung fIon IoI hon cac su dung Command line. ÐoI vì. cho
du ban dIou khIon MySQL duoI baf ky hình fhuc nao. muc dích cung quan Iy va fhao fac co so du
IIou.
2. CA¡ OAT MYSQL
Ðo caI daf MySQI fron non WIndovs ban fhoo cac buoc sau:
ƒ Truoc fIon ban chop fap fIn mysqI-4.0.0a-aIpha-vIn.zIp vao dIa cung hoac chon chung fu dIaq
CÐ va gIaI non fap fIn
ƒ Chay fap fIn Sofup.oxo. chon dIa C hay Ð
ƒ Sau khI caI daf fhanh cong. ban kIom fra frong WIndovs SorvIcos xuaf hIon dIch vu mySQI
hay khong7. Ðo su dung duoc MySQI fhì frang fhaI cua dIch vu nay phaI o cho do Sfarfod.
Iuu y rang. frong fruong hop MySQI khong fho chay duoc. do dIch vu cua MySQI chua Sfarfod
nhu . do co fho chay duoc MySQI fhì ban can mof so fhay doI frong fap fIn my.InI frong fhu muc
WINNT
--------------------------------------------------------------------------------------
=TDi: Fiì- w¬: .¬c- o:iDo tD- 'iD\v:OLAc.iD ì.·
=Tccì
='ìì2DD· ìD:cD:ì· A\
=LD·c..-Dt cr Acc cDìv tD- ×-v: tD¬t vco ×Dcw Dcw wcr×:.
=F-¬c tD- \v:OL \¬Do¬ì 1cr iD:tro·ticD:
.v:cìc÷Dt`
r¬:-cirC:.v:cì
=riDc÷¬ccr-::ì2¯.D.D.ì
c¬t¬cirC:.v:cìc¬t¬
=ì¬Doo¬o-C:.v:cì:D¬r-vcor ì¬Doo¬o- cir-·tcrv
=:ìcw co-rv ìco=
=t.rcir=
=rcrt··Dt
=:-t÷¬ri¬rì-×-v_ro11-rìt\
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

'iD\v:OL¬c.iD`
:-r-rC:.v:cìriD.v:cìc÷Dt.-×-
o:-rrcct
r¬::wcrc
Oo-rvíDt-r¬ììD

3. TAO CO SO O¡ L¡K¡ VA NG¡O¡ O¡NG
Trong fruong hop ban su dung gIao dIon do hoa fhì dung ích quan frI co so du IIou MySQI. ban
co fho chay fap fIn mysqlfront.exe frong fhu muc MySQL Control. bang cach chay fap fIn cua so xuaf
hIon nhu hình 8-1. Nou Ian dau fIon fao kof noI co so du IIou. ban can phaI fao mof Connection. cung
cap fon Server hay IP cua may chua MySQL.
Tuy nhIon. frong fruong hop may chua co so du IIou MySQL Ia may dang su dung. ban co fho
su dung localhost. NgaI ra. cung gIong nhu cac co so du IIou khac. Username mac dInh cua co so du
IIou MySQl Ia root va Password Ia rong.
Nou ban da co co so du IIou dang fon faI. ban co fho go fon co so du IIou frong phan Databases |
nou muon mo nhIou database. ban co fho dung dau ; do phan cach).
Trong fruong hop Ian dau fIon. ban khong can cung cap fon co so du IIou. ban co fho fao chung
sau khI kof noI.


Hình 8-1: Kof noI co so du IIou bang MySQIIronf TooI
Sau kof noI co so du IIou fhanh cong. frình dIou khIon co so du IIou MySQL co gIao dIon nhu
hình 8-2. cong vIoc dau fIon ban phaI fhuc hIon Ia fao co so du IIou.
Ðaf dau fu menu co fon Tools 1 Create Database hay chon fon root@localhost | R-Click | Create
Database. cua so xuaf hIon nhu hình 8-3.

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM





Hình 8-2: CIao dIon dIou khIon co so du IIou MySQI
Cung cap fon co so du IIou. frong fruong hop nay ban co fho nhap Test. bam nuf OK. co so du
IIou xuaf hIon frong cua so dIou khIon.






Hình 8-3: Tao co so du IIou co fon Tosf
Trong ca haI fruong hop fao co so du IIou bang MySQL fhanh cong nhu fron. ban co fho fìm
fhay fon co so du IIou do frong fhu muc mysql/data nhu hình 8-4 sau:

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Hình 8-4: Thu muc fIn co so du IIou Tosf
3.1. Quun ly nguo¡ dung
Iam fho nao do dang nhao vao co so du IIou MySQL. ban co fho su dung haI cach nhu frình
bay o fron. Tuy nhIon. sau khI fao ra cac username khac. ban co fho su dung chung do dang nhap.
Ðo dang nhap vao MySQL bang Command line. ban chI can go >mysql – hostname –u username
– p fu dau nhac hay dang nhap bang cach su dung frình gIao dIon do hoa. Tu khoa -h hI ra rang fon
|computer name). IP. hay localhost cua may co su dung co so du IIou MySQL. -u chI ra rang ban su
dung username. username Ia fon username. -p duoc chI dInh khI username nay co password. Trong
fruong hop password Ia rong. ban co fho khong cung cap fham so –p.
Ðo fao User frong co so du IIou MySQL. ban co fho su dung haI cach fron. Nou ban fhuc hIon
vIoc fao mof Username bang Command line. ban co fho go fu dau nhac nhu phaf bIou sau:

CFANT
:-ì-·t, íD:-rt, Lrc¬t-,
L-ì-t-, íDc-×, Aìt-r,
Cr-¬t-, Lrcr, F-1-r-D·-:
ON *.* TO `.vi:`c`°`
íLFNTíFíFL FY `ì2·4ct¯º`

Trong phaf bIou fron. vua fao ra User co fon myis. voI hostname Ia co so du IIou hIon hanh.
password Ia 1234 va duoc cac daf quyon Select, Insert, Update, Delete, Index, Alter, Create. Drop fron
co so du IIou hIon hanh.
Trong fruong hop ban fao ra mof Username khong cung cap cac daf quyon fron co so du IIou.
ban co fho fhuc hIon nhu phaf bIou fao username: test, password: 1234 sau:

CFANT
o:¬o-
ON *.* TO `t-:t`c`°`
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

íLFNTíFíFL FY `ì2·4`

Nou ban su dung gIao dIon do hoa. ban co fho fao username va gan quyon nhu fron bang cach
su dung menu co fon Tools 1 User-Manager. cua so xuaf hIon nhu hình 8-5.



Hình 8-5: Tao !sornamo
3.2. Cup quyen cho nguo¡ dung
Cac daf quyon Select, Insert, Update, Delete, Index, Alter, Create. Drop fron co so du IIou. ban
co fho fham khao chI fIof frong bang 8-1.
Bung S-J: Cuc Jul quyen lren co so Ju Iieu
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i ¬r coDo Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
:-ì-·t t¬rì-:, CDc rD-r user trov ¬D .¬o
·cìo.D: tiD td Table.
iD:-rt t¬rì-:, CDc rD-r user tD-. .ci .¬o
·cìo.D: tiD ¬c Table.
orc¬t- t¬rì-:, CDc rD-r user tD¬v ¹ci oi¬
·cìo.D: tri ·o¬ .¬o tiD tcD t¬i
trcDo Table.
c-ì-t- t¬rì-: CDc rD-r o:-r .¬o tiD tcD
t¬i trcDo Table.
iDc-× t¬rì-: CDc rD-r user tD-. .ci D¬v
×c¬ ·Di .o· ·o¬ Table.
¬ìt-r t¬rì-: CDc rD-r user tD¬v ¹ci ·¬o
tro· ·o¬ ¹ci tdcDo Table
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

D¬v L¬t¬r¬:- tcD t¬i, DDd
tD-. ·ct ¬c trcDo Table
tcD t¬i, tD¬v ¹ci ×i-o cd
ìi-o ·o¬ ·ct cd ìi-o, ..
·r-¬t- c¬t¬r¬:-: CDc rD-r user t¬c .ci ¹ci
t¬rì-: tdcDo Table D¬v L¬t¬r¬:-.
crcr c¬t¬r¬:-: CDc rD-r o:-r ×c¬ ¹ci
t¬rì-: tdcDo Table D¬v Database.
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

Xuaf phaf fu cac quyon co anh huong don cau fruc co so du IIou. cac doI fuong cua co so du IIou
va du IIou. ban co fho xom xof ky cang fruoc khI cap quyon cho user am vIoc fron co so du IIou.
NgoaI cac quyon fron. frong MySQL con co mof so quyon khong gan mac dInh nhu frong bang
8-2. ban co fho xom xof cac daf quyon quan frI do cap cho nguoI dung.
Bung S-2: Cuc Jul quyen quun lri lren co so Ju Iieu
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
r-ìc¬c CDc rD-r Dodci co¬D tri D¬r ì¬i ·¬·
Table, cov-D, host, logs ¬ Table.
:DotccwD CDc rD-r Dodci co¬D tri ·D¬. cdt Dc¬t
¹cDo MySQL Server.
rrc·-:: CDc rD-r Dodci co¬D tri ×-. co¬ triDD
tDd· Di-D ·o¬ triDD ·Do ¬ ·c tD- ·D¬.
cdt .ct :c co¬ triDD ¹¬Do tDd· tDi.
1iì- CDc rD-r cd ìi-o oDi ¬c Table td t¬r
tiD.
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

Iuu y: Nhung username bình fhuong khong non cap quyon nhu frong bang 8-2 cho ho. frong
fruong hop ban muon cap faf ca cac quyon frong bang 8-1 va Ðang 8-2 cho username khI fao ra ho.
ban Table su dung fu khoa All fhay vì All Privileges frong phaf bIou fao user nhu sau:

CFANT
ALL
ON *.* TO `-×D¬Do`c`°`
íLFNTíFíFL FY `ì2·4ct¯º`

Tuong fu nhu vay. frong fruong hop ban khong cung cap baf ky daf quyon nao fron co so du
IIou hIon hanh. ban co fho khaI bao phaf bIou cap uyon nhu sau:

CFANT
o:¬o-
ON *.* TO `-×D¬Do`c`°`
íLFNTíFíFL FY `ì2·4ct¯º`

3.3. Xou quyen cuu user
Ðo xoa cac quyon cua user fu co so du IIou hIon hanh. ban co fho su dung phaf bIou SQL co fon
Revoke. phaf bIou Revoke nguoc IaI voI phaf bIou Grant.
Nou ban xoa mof so quyon cua user. ban co fho su dung khaI bao nhu phaf bIou sau:

F-c×- rriiì-o-: (·cìo.D:)`
ON it-.
Frc. o:-rD¬.-

Trong fruong hop xoa faf ca cac quyon cua user. ban co fho su dung phaf bIou nhu sau:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


F-c×- Aìì
ON it-.
Frc. o:-rD¬.-

Nou user do duoc cap quyon voI fuy chon Grant Option. do xoa cac quyon do cua user. ban co
fho khaI bao nhu sau:

F-c×- Cr¬Dt OrticD
ON it-.
Frc. o:-rD¬.-

Ðo fham khao chI fIof qua frình cap va xoa quyon cua mof user. ban co fho fham khao mof so
phaf bIou nhu sau:
Can quyon Administrator cho user co fon fred fron moI co so du IIou frong MySQL. password
cua anh fa Ia mnb123. ban co fho khaI bao nhu sau:

Cr¬Dt ¬ìì
OD *
Tc 1r-c iDc-ti1v-c rv ‘.Drì2·’
'itD Cr¬Dt OrticD;

Nou ban khong muon user co fon fred frong ho fhong. ban co fho xoa anh fa bang cach khaI
bao phaf bIou sau:

F-c×- ¬ìì
OD *
Frc. 1r-c;

Tao mof user co fon ekhang voI password Ia 12345678. duoc Iam vIoc fron co so du IIou Test.
khong cap quyon cho user nay. ban co fho khaI bao nhu sau:

Cr¬Dt o:¬o-
OD T-:t.*
Tc -×D¬Do ic-Dti1i-c rv ‘ì2·4ct¯º’;

Tuong fu nhu vay. frong fruong hop ban muon cap mof so quyon cho user co fon ekhang fron co
so du IIou Test. ban co fho khaI bao nhu sau:

Cr¬Dt :-ì-·t, iD:-rt, c-ì-t-, orc¬t-, iDc-×, crcr
OD T-:t.*
Tc -×D¬Do;

Nou ban muon xoa bof mof so quyon cua user co fon ekhang fron co so du IIou Test. ban co fho
khaI bao nhu sau:

F-c×- orc¬t-, c-ì-t-, crcr
OD T-:t.*
Frc. -×D¬Do;

Nhung frong fruong hop xoa faf ca cac quyon cua user co fon ekhang fron co so du IIou Test.
ban co fho khaI bao:

F-c×- Aìì
OD T-:t.*
Frc. -×D¬Do;
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


4. K¡K¡ O¡ L¡K¡ C¡A CO SO O¡ L¡K¡ MYSQL
Truoc khI fhIof ko co so du IIou fron MySQL. ban can phaI fham khao mof so kIou du IIou
fhuong dung. chung bao gom cac nhom nhu: numeric, date and time va string.
Ðou can Iuu y frong khI fhIof ko co so du IIou. ban can phaI xom xof kIou du IIou cho mof cof
frong Table sao cho phu hop voI du IIou cua fho gIoI fhuc.
ÐIou nay co nghIa Ia khI chon du IIou cho cof frong Table. ban phaI xom xof don IoaI du IIou
can Iuu fru fhuoc nhom kIou du IIou nao. chIou daI cung nhu cac rang buoc khac. nham khaI bao cho
phu hop.
4.1. Lou¡ du l¡eu numer¡c
KIou du IIou numeric bao gom kIou so nguyon frình bay frong bang 8-3 va kIou so cham dong.
frong fruong hop du IIou kIou dau cham dong ban can phaI chI ro bao nhIou so sau dau phan Io nhu
frong bang 8-4.
Bung S-J: Kieu Ju Iieu so nguyen
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i F¬Do- Fvt-: Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
tiDviDt ÷ì2¯÷>ì2º ì :c Doov-D r¬t DDc.
D¬v
D..2cc

:.¬ììiDt ÷·2¯tº 2 :c Doov-D DDc.
÷>·2¯t¯
D¬v
D..tcc·c

.-cio.iDt ÷º·ººtDº · :c Doov-D d¬.
÷> º·ººtD
D¬v
D..ìt¯¯¯2ìc

iDt ÷2
·ì
÷>2
·ì
֓ 4 :c Doov-D.
D¬v
D..2
·2
֓

rioiDt ÷2

÷>2

÷ì º :c Doov-D ìcD.
D¬v
D..2
t4
֓
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

Bung S-4: Kieu Ju Iieu so chum Jong
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i F¬Do- Fvt-: Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
1ìc¬t rDo tDoc· :c tD¬r rD¬D
:c tD¬r c¬Do Single D¬v
JD¬D Double.

Fìc¬t(\,L) 4 :c tD¬r rD¬D
±ì.ì¯c4'4·cìF÷·º c¬Do :ingle.
±·.4D2º2·4tt·º

Lcorì-(\,L) º :c tD¬r rD¬D
c¬Do Double.
±ì.¯'¯t'·ì·4ºt2·ìc¯·Dº
±2.22cD¯·ºcºcD¯2Dì4F÷·Dº
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Fìc¬t(\,L`)
:c ·D¬. ¹cDo ìdo
cdci c¬Do char.
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

4.2.
4.3.
Lou¡ du l¡eu Outet und T¡me
KIou du IIou Date and Time cho phop ban nhap IIou duoI dang chuoI hay dang so nhu frong
bang 8-5.
Bung S-5: Kieu Ju Iieu so nguyen
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i F¬Do- Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
L¬t- ìDDD÷Dì÷Dì Date triDD r¬v cdci c¬Do
vvvv÷..÷cc.

Ti.- ÷º·º:c':c' Time triDD r¬v cdci
º·º:c':c' c¬Do DD:..:::.

L¬t-Ti.- ìDDD÷Dì÷Dì Date ¬ Time triDD r¬v cdci
DD:DD:DD c¬Do vvvv÷..÷cc DD:..:::.
''''÷ì2÷·ì
2·:c':c'

Ti.-:t¬.r(\)`
ì'¯D÷Dì÷Dì Ti.-:t¬.r triDD r¬v cdci
DD:DD:DD c¬Do vvvv÷..÷cc DD:..:::.
Y-¬r(24)`
ì'¯D÷2Dt' Y-¬r triDD r¬v cdci
ì'Dì÷2ìcc c¬Do 2 :c D¬v 4 :c.
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

ÐoI voI kIou du IIou TimeStamp. ban co fho dInh dang nhIou cach nhu frình bay frong bang 8-
6.
Bung S-6: Trình buy Jui Jien cuu TimeSlump
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i Ii-D tDi
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Ti.-:t¬.r YYYY\\LLII\\::
Ti.-:t¬.r(ì4) YYYY\\LLII\\::
Ti.-:t¬.r(ì2) YY\\LLII\\::
Ti.-:t¬.r(ìD) YY\\LLII\\
Ti.-:t¬.r(º) YYYY\\LL
Ti.-:t¬.r(t) YY\\LL
Ti.-:t¬.r(4) YY\\
Ti.-:t¬.r(2) YY
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

Lou¡ du l¡eu Str¡ng
KIou du IIou String chIa Iam ba IoaI. IoaI fhu nhaf nhu char |chIou daI co dInh) va varchar
|chIou daI bIon fhIon). Char cho phop ban nhap IIou duoI dang chuoI voI chIou daI Ion nhaf bang
chIou daI ban da dInh nghIa. nhung khI fruy cap du IIou fron Field co khaI bao dang nay. ban can
phaI xu Iy khoang frang. ÐIou nay co nghIa Ia nou khaI bao chIou daI Ia 10. nhung ban chI nhap
hcuoI 4 ky fu. MySQL Iuu fru frong bo nho chIou daI 10.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Nguoc IaI voI kIou du IIou Char Ia Varchar. chIou daI Ion haf nguoI dung co fho nhap vao bang
chIou daI ban da dInh nghIa cho Field nay. bo nho chI Iuu fru chIou daI dung voI chIou daI cua chuoI
ban da nhap.
Nhu vay. co nghIa Ia nou ban khaI bao kIou varchar 10 ky fu. nhung ban hcI nhap 5 ky fu.
MySQL chI Iuu fru chIou daI 5 ky fu. ngoaI ra. khI ban fruy cap don Field co kIou du IIou nay. ban
khong can phaI gIaI quyof khoang frang.
IoaI fhu haI Ia Text hay Blob. Text cho phop Iuu chuoI raf Ion. Blob cho phop Iuu doI fuong nhI
phan. IoaI fhu 3 Ia Enum va Set. Ðan co fho fham khao ca ba IoaI fron frong bang 8-7.
Bung S-7: Kieu Ju Iieu Slring
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
Lc¬i F¬Do- Li-D oi¬i
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
·D¬r ì÷2cc CDi-o c¬i ·o¬ ·Doci ìcD DD¬t
·D¬r¬·t-r: 2cc ×v td.

¬r·D¬r ì÷2cc CDi-o c¬i ·o¬ ·Doci ìcD DD¬t
·D¬r¬·t-r: 2cc ×v td (characters).

tiDvrìcr 2
º
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
¹ci tdcDo DDi rD¬D ·c 2cc
characters.

tiDvt-×t 2
º
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
·Doci ·c 2cc characters.

rìcr 2
ìt
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
blob ·c tc,c·c characters..

t-×t 2
ìt
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
·Doci c¬Do ¬D r¬D ·c tc,c·c
characters.
\-cio.rìcr
2
24
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
rìcr d¬ ×Dc¬Do ìt,¯¯¯,2ìc
characters.

\-cio.t-×t
2
24
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
·Doci c¬Do ¬D r¬D d¬
×Dc¬Do ìt,¯¯¯,2ìc
characters.
LcDorìcr
2
·2
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
blob ìcD ×Dc¬Do
4,2'4,'t¯,2'c characters.

LcDot-×t
2
·2
÷ì KD¬i r¬c ·Dc Field ·Dd¬ ×i-o
·Doci c¬Do ¬D r¬D ìcD
×Dc¬Do 4,2'4,'t¯,2'c
characters.
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷

5. PHAT B¡K¡ SQL
MySQL Ia mof ho fhong quan Iy co so du IIou quan ho |RDBMS) hay con duoc goI Ia Relational
Database Management System. RDBMS Ia mof frong nhung mo hình co so du IIou quan ho fhong
dung hIon nay.
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

5.1.
5.2.
5.2.1.
Nhom phut b¡eu SQL
Nhu da frình bay frong chuong 3. hau hof san pham co so du IIou quan ho hIon nay dou dua
fron chuan cua SQL va ANSI-SQL. chang han nhu SQL Server. Oracle. PostgreSQL va MySQL. ÐIou
nay co nghIa Ia faf ca nhung co so du IIou quan ho dou phaI co nhung fIou chuan fhoo cu phap SQL
va MySQL cung khong phaI Ia ngoaI Io.
Ngon ngu SQL chIa Iam 4 IoaI sau:
¾ DDL (Data Definition Language): Ngon ngu dInh nghIa du IIou. dung do fao co so du IIou.
dInh nghIa cac doI fuong co so du IIou nhu Table. Query. Views hay cac doI fuong khac.
¾ DML (Data Manipulation Language): Ngon ngu fhao fac du IIou. dung do fhao fac du IIou.
chang han nhu cac phaf bIou: Select, Inert, Delete, Update, ...
¾ DCL: (Data Control Language): Ngon ngu su dung fruy cap doI fuong co so du IIou. dung do
fhay doI cau fruc. fao nguoI dung. gan quyon chang han nhu: Alter, Grant, Revoke, ...
¾ TCL: (Transaction Control Language): Ngon su dung do khaI bao chuyon fac chang han nhu:
Begin Tran, Rollback, Commit, ...
Phut b¡eu SQL thuo tuc du l¡eu
Phaf bIou SQL bao gom cac IoaI nhu sau:
¾ SELECT (Truy vaán maåu tin).
¾ INSERT (Theâm maåu tin).
¾ UPDATE (Caäp nhaät döõ lieäu).
¾ DELETE (Xoaù mau fIn).
Khu¡ n¡em co bun ve Select
Phaf bIou Select dung do fruy van du IIou fu mof hay nhIou bang khac nhau. kof qua fra vo Ia
mof fap mau fIn fhoa cac dIou kIon cho fruoc nou co. cu phap cua phaf bIou SQL dang SELECT:
:FLFCT <c¬DD :¬·D ·¬· ·ct>
FFO\ <c¬DD :¬·D r¬Do>`
'IFFF <·¬· ¹i-o ×i-D r¬Do roc·>`
CFOLJ FY <t-D ·ct ri-o tDd· trcDo :FLFCT> `
IAVíNC <¹i-o ×i-D r¬t roc· ·o¬ CFOLJ FY>`
OFLFF FY <c¬DD :¬·D ·ct>`
Lí\íT Frc.No.r-r TcNo.r-r`

Ðanh sach cac cof: KhaI bao cac fon cof. bIou fhuc kof hop gIua cac cof cua Table ban can fruy
Iuc. Trong fruong hop co haI cof cung fon cua haI Table frong phaf bIou. ban can phaI chI dInh fon
Table dI fruoc. Chang han. nhu ví du 8-1.
VI du 8-1: Phut b¡eu SKLKCT

:-ì-·t ít-.íL,ít-.N¬.-
Frc. trìít-.:
'D-r- Cc:t>ìDD;

:-ì-·t trìOrc-r:.Orc-ríL,Orc-rL¬t-,ít-.íL,Ottv
Frc. trìOrc-r:,trìOrc-rL-t¬iì:
'D-r- trìOrc-r:.Orc-ríL _ trìOrc-rL-t¬iì.Orc-ríL;

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

5.2.2. Phut b¡eu SKLKCT vo¡ menh de FROM
Phaf bIou SQL dang SELECT Ia mof frong nhung phaf bIou you cau MySQL fruy Iuc du IIou
fron co so du IIou chI dInh. SELECT dung do doc fhong fIn fu co so du IIou fhoo nhung fruong quy
dInh. hay nhung bIou fhuc cho fruong do.
Monh do FROM chI ra fon mof bang hay nhung bang co quan ho can fruy van fhong fIn.
Thuong chung fa su dung cong cu MySQL-Front | Query do fhuc fhI phaf bIou SQL.
Sau khI fhuc fhI phaf bIou SQL. kof qua fra vo so mau fIn va fong so mau fIn duoc Iay ra fu
bang.
Ðau ` cho phop Ioc mau fIn voI faf ca cac fruong frong bang. nou muon chI ro nhung fruong nao
can Ioc ban can nou fon cu fho nhung fruong do.
Ðo fIon fham khao frong gIao frình nay chung foI su dung mof phan co so du IIou co san cua
MySQL. dong fhoI bo sung fhom co so du IIou danh cho ung dung ban hang qua mang.
Co so du IIou ban hang qua mang co fon Ia Test. va bao gom nhIou bang. Ðang phaf bIou
SELECT chung fa co fho bIof so bang hay doI fuong khac dang co frong co so du IIou Test

VI du 8-2: Thuc th¡ phut b¡eu SQL SKLKCT he thong

:Dcw t¬rì-:
1rc. T-:t
/* Hieån thò taát caû teân baûng cuûa cô sôû döõ lieäu hieän haønh */

Kof qua fra vo danh sach bang nhu sau:

TAÐIÐS_IN_TÐST
--------------------------------------
trìCcoDtri-:
trìJrciD·-:
trìAotDcr:

trìJ¬v.-Dt
trìít-.:icD
trìCo:tc.-r:
trì:c1tw¬r-

Gh¡ chu:
Baïn coù theå söû duïng phaùt bieåu SQL treân ñeå hieån thò nhöõng ñoái töôïng trong cô sôû döõ lieäu, baèng caùch thay
theá caùc tham soá vaø ñieàu kieän.
Cu phup don g¡un
:-ì-·t *
Frc. t¬rì-D¬.-
/* Loïc taát caû soá lieäu cuûa taát caû caùc coät (field) cuûa tablename*/

:-ì-·t 1i-ìcì,1i-ìc2
Frc. t¬rì-D¬.-
/* Loïc taát caû soá lieäu cuûa 2 field: field1, field2 cuûa tablename*/

:-ì-·t *
Frc. t¬rì-D¬.-
Li.it D,ìD
/* Loïc top 10 maåu tin ñaàu tieân cuûa taát caû caùc field cuûa tablename*/

:-ì-·t 1i-ìcì, 1i-ìc2
Frc. t¬rì-D¬.-
Li.it D,ìD
/* Loïc top 10 maåu tin ñaàu tieân cuûa 2 fields field1, field2 cuûa
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

tablename*/

VI du 8-3: phut b¡eu phut b¡eu SQL dung Select

:-ì-·t *
Frc. trìCcoDtri-:
/* Lieät keâ taát caû caùc quoác gia trong baûng tblCountries hoaëc baïn coù theå lieät keâ teân nhö phaùt bieåu sau */

:-ì-·t CcoDtrvN¬.-
Frc. trìCcoDtri-:

Kof qua fra vo nhu sau:

CcoDtrvCcc- CcoDtrvN¬.-
÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷
VNA Vi-tD¬.
:NC :iDo¬rcr-
L:: LDit-c :t¬t-c
LKL LDit-c KiDocc.
CFF C-r.¬Dv
CA\ C¬.rcci¬
TIA TD¬i L¬Dc
\AL \¬ì¬v:i¬
íNC íDccD-:i¬
CIN CDiD¬

5.2.3. Phut b¡eu SQL dung SKLKCT vo¡ menh de Where
KhI ban dung monh do WHERE do fao non fIou chuan can Ioc mau fIn fhoo fIou chuan duoc
dInh nghIa. fhong fhuong WHERE dung cof |fruong) do so sanh voI gIa frI. cof khac. hay bIou fhuc
chua cof |fruong) baf ky co frong bang. Phaf bIou SQL dang Select voI monh do Where cu phap co
dang nhu sau:

:-ì-·t *
1rc. t¬rì-D¬.-
wD-r- ·cDciticD:

:-ì-·t 1i-ìcì, 1i-ìc2, 1i-ìc·
1rc. t¬rì-D¬.-
wD-r- ·cDciticD:
VoI conditions frong ca haI phaf bIou fron duoc dInh nghIa dIou kIon fruy van nhu khaI bao sau:

:-ì-·t *
Frc. t¬rì-D¬.-
wD-r- 1i-ìcì>ìD

:-ì-·t *
1rc. trìCcoDtri-:
wD-r- CcoDtrvCcc- iD(`VNA`,`CIN`)

Cuc phep toun so sunh trong conJilions buo gom:
♦ > : Ion hon vhoro Amounf > 100000;
♦ < : nho hon vhoro Amounf < 100000;
♦ >= : Ion hon hoac bang vhoro Amounf >= 100000;
♦ >= : nho hon hoac bang vhoro Amounf <= 100000;
♦ = : bang vhoro CusfIÐ=’12';
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

♦ != : Khac vhoro CusfIÐ!='12';
♦ <> : Khac vhoro CusfIÐ<>'12';

Cuc phep toun Iogic co the su dung trong conJilions

♦ and : Phop foan "and"

:FLFCT *
FFO\ trìOrc-r:
'D-r- A.coDt.>ìDDDDD
ADc Co:tíL`ì2`;

♦ Or : Phop foan "or"

:FLFCT *
FFO\ trìOrc-rL-t¬iì:
'D-r- A.coDt.>ìDDDDD
Or Co:tíL‘ì2’;

♦ Not : Phop foan phu dInh |not)

:FLFCT *
FFO\ trìOrc-r:
wD-r- Orc-rL¬t- i: Dct Doìì;

♦ Not in : Phop foan phu dInh |not in)

:FLFCT *
FFO\ trìOrc-r:
wD-r- Orc-ríL Dct iD (‘ì2’,’ìc’);

♦ Between: Kof qua fhuoc frong mIon gIa frI

:FLFCT *
FFO\ trìOrc-r:
'D-r- A.coDt r-tw--D ìD
ADc cDD;

♦ Like : Phop foan so sanh gan gIong. su dung dau % do fho hIon fhay fho bang ky fu daI
dIon

:FLFCT *
FFO\ trìCo:tc.-r:
wD-r- Co:tN¬.- ìi×- `°A`;

♦ Not Like : Phop foan phu dInh so sanh gan gIong. su dung dau % do fho hIon fhay fho
bang ky fu daI dIon

:FLFCT *
FFO\ trìCo:tc.-r:
wD-r- Co:tN¬.- Dct ìi×- `°A`;

♦ IN : Phop foan so sanh frong mof fap hop

:FLFCT *
FFO\ trìOrc-r:
'D-r- Orc-ríL iD (`ìDD`,`2DD`,`·DD`);

VI du 8-5: VI du ve SQL dung SKLKCT vu Where
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

/* > : lôùn hôn */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt > ìDDDDD;

/* < : nhoû hôn */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt < ìDDDDD;

/* >= : lôùn hôn hoaëc baèng */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt > ìDDDDD;

/* >= : nhoû hôn hoaëc baèng */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt < ìDDDDD;

/* = : baèng */

:-ì-·t *
Frc. trìOrc-r:
'D-r- Co:tíL‘ì2’;

/* != :Khaùùc */

:-ì-·t *
Frc. trìOrc-r:
'D-r- Co:tíL .‘ì2’;

/* <> : Khaùc */

:-ì-·t *
Frc. trìOrc-r:
'D-r- Co:tíL <>‘ì2’;

/* !> : Khoâng lôùn hôn */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt .> ìDDDDD;

/* !< : Khoâng nhoû hôn */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt .< ìDDDDD;

-- Cac phop foan IogIc
/* and : Pheùp toaùn vaø */

:-ì-·t *
Frc. trìOrc-r:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

'D-r- A.coDt .>ìDDDDD
ADc Co:tíL‘ì2’;

/* Or : Pheùp toaùn hoaëc */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt .>ìDDDDD
Or Co:tíL‘ì2’;

/* Not : Pheùp toaùn phuû ñònh */

:-ì-·t *
Frc. trìOrc-r:
'D-r- Orc-rL¬t- i: NOT NLLL;

/* Between: giaù trò naèm trong mieàn */

:-ì-·t *
Frc. trìOrc-r:
'D-r- A.coDt
F-tw--D ìD ¬Dc cDD;

/* Like : Pheùp toaùn so saùnh gaàn gioáng, söû duïng daáu %
ñeå theå hieän thay theá baát kyø kyù töï */

:-ì-·t *
Frc. trìOrc-r:
'D-r- L-:·riicD ìi×- `°A`
Or Co:tíL `ìc2`;

/* Not Like : Pheùp toaùn phuû ñònh so saùnh gaàn gioáng,
söû duïng daáu % ñeå theå hieän thay theá baát kyø kyù töï */

:-ì-·t *
Frc. trìOrc-r:
'D-r- L-:·riicD Dct ìi×- `°A`
Or Co:tíL `ìc2`;

/* IN : Pheùp toaùn so saùnh trong moät taäp hôïp */

:-ì-·t *
Frc. trìOrc-r:
'D-r- Orc-ríL iD (`ì·4`,`244`,`4··`);

/* Not IN : Pheùp toaùn phuû ñònh so saùnh trong moät taäp hôïp */

:-ì-·t *
Frc. trìOrc-r:
'D-r- Orc-ríL Dct iD (`ì·4`,`244`,`4··`);



CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

5.2.4. Menh de Order by
Thong fhuong. frong khI fruy van mau fIn fu bang du IIou. kof qua hIon fhI can sap xop fhoo
chIou fang hay gIam dua fron ky fu ALPHABET. Nhung ban cung co fho sap xop fhoo mof fIou chuan
baf ky. chang han nhu bIou fhuc.
KhI sap xop du IIou frình bay frong kof qua. can phaI chon fruong hay bIou fhuc fhoo fraf fu
fang dan hoac gIam dan.
Cu phap cho monh do ORDER BY cung voI frang fhaI fang hay gIam. ung voI ASC sap xop
fang dan. DESC gIam dan.
Cu phup co dung nhu suu:

Orc-r rv ·cìo.DD¬.- LF:C
Orc-r rv ·cìo.DD¬.-ì + ·cìo.DD¬.-2 LF:C
Orc-r rv ·cìo.DD¬.- A:C
Orc-r rv ·cìo.DD¬.-ì A:C, ·cìo.DD¬.-2 LF:C

VI du 8-6: SKLKCT vo¡ menh de Order by OKSC

/*-- Giaûm daàn theo thôøi gian */
:-ì-·t Orc-ríL , Orc-rL¬t-, Co:tíL, A.coDt
Frc. trìOrc-r:
'D-r- A.coDt >ìDDD
Orc-r rv Orc-rL¬t- LF:C

Kof qua fra vo nhu sau:

Orc-ríL Orc-rL¬t- Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì¯ 2DDì÷D'÷2D ì2 쯺.24·
ìº 2DDì÷D'÷2D ì2 2.¯ºc·4
ìt 2DDì÷D'÷ì' ì2 ·'º.¯'º
ìc 2DDì÷D'÷ìº ì2 c.¯cº.º¯t
ì4 2DDì÷D'÷ì¯ ì2 c.c·'.t4¯
ì2 2DDì÷D'÷ìt ì2 ì.··D
ì· 2DDì÷D'÷ìt ì2 ì.cºc.ct·
·ì 2DDì÷D'÷ìt ì· 4c'.c2c
ìì 2DDì÷D'÷ìc ìì ì.4Dì.ºD·
2º 2DDì÷D'÷ìc ì· ì.4c2DD

VI du 8-?: SQL dung SKLKCT vo¡ menh de Order by vu ASC
/*-- Taêng daàn theo thôøi gian */
:-ì-·t Orc-ríL , Orc-rL¬t-, Co:tíL, A.coDt
Frc. trìOrc-r:
'D-r- A.coDt >ìDDD
Orc-r rv Orc-rL¬t- A:C

Kof qua fra vo nhu sau

Orc-ríL Orc-rL¬t- Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷
Dì 2DDì÷D'÷Dc ìD 2.'D·.c¯t
D2 2DDì÷D'÷Dc ìD 4º.ìtº.ct¯
D· 2DDì÷D'÷Dc ìD c.ìD¯.D·2
D4 2DDì÷D'÷Dº ìD 2.·cc.c·¯
Dc 2DDì÷D'÷Dº ìt ì.ºì¯.4º¯
Dt 2DDì÷D'÷ìD ìt 2t.DDD
ì' 2DDì÷D'÷ìD ì2 c¯c.tt¯
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

2' 2DDì÷D'÷ìD ì· 4tt.cDD
D¯ 2DDì÷D'÷ìì ìt ìºt.¯º2
2· 2DDì÷D'÷ìì ì2 4c'.ìt2

Nou muon sap xop fhoo nhIou cof |fruong). chI can su dung dau phay |.) do phan cach cac cof.
VI du 8-?: SKLKCT vo¡ menh de Order by vo¡ 2 cot du l¡eu

:-ì-·t Orc-ríL , Orc-rL¬t-, Co:tíL, A.coDt
Frc. trìOrc-r:
'D-r- A.coDt >ìDDD
Orc-r rv Orc-ríL,Co:tíL LF:C

Kof qua fra vo nhu sau:

Orc-ríL Orc-rL¬t- Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷
·ì 2DDì÷D'÷ìt ì· 4c'.c2c
·D 2DDì÷D'÷ìc ì· ìc·.ì2D
2' 2DDì÷D'÷ìD ì· 4tt.cDD
2º 2DDì÷D'÷ìc ì· ì4c.2DD
2¯ 2DDì÷D'÷ì4 ì· tD·.D··
2t 2DDì÷D'÷ì· ì· 2·D.DDD
2c 2DDì÷D'÷ìì ì· 244.'D4
24 2DDì÷D'÷ì2 ì· ì.·t¯.22º
2· 2DDì÷D'÷ìì ì2 4c'.ìt2
ì' 2DDì÷D'÷ìD ì2 c¯c.tt¯

Nou muon sap xop fhoo nhIou fruong kof hop. chI can dung fhu fu fung cof cach nhau bang dau
+.
VI du 8-8: SKLKCT vo¡ menh de Order by hop 2 cot

/*-- Giaûm daàn theo soá OrderID vaø CustID */
:-ì-·t Orc-ríL , Orc-rL¬t-, Co:tíL, A.coDt
Frc. trìOrc-r:
'D-r- A.coDt >ìDDD
Orc-r rv Orc-ríL + Co:tíL LF:C

Kof qua fra vo nhu sau:

Orc-ríL Orc-rL¬t- Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷
·ì 2DDì÷D'÷ìt ì· 4c'.c2c
·D 2DDì÷D'÷ìc ì· ìc·.ì2D
2' 2DDì÷D'÷ìD ì· 4tt.cDD
2º 2DDì÷D'÷ìc ì· ì4c.2DD
2¯ 2DDì÷D'÷ì4 ì· tD·.D··
2t 2DDì÷D'÷ì· ì· 2·D.DDD
2c 2DDì÷D'÷ìì ì· 244.'D4
24 2DDì÷D'÷ì2 ì· ì.·t¯.22º
2· 2DDì÷D'÷ìì ì2 4c'.ìt2
ì' 2DDì÷D'÷ìD ì2 c¯c.tt¯

Nou frong phaf bIou SQL dang SELECT co nhIou bang kof hop IaI voI nhau. ban co fho dung
fhom fon bang ung voI cof cua bang do. Phan nay so duoc dIon gIaI cu fho hon frong phan ko fIop
|JOIN -Phop hop).
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

5.2.5. SQL dung SKLKCT vo¡ menh de GRO¡P BY
KhI fruy van mau fIn fron mof hay nhIou bang du IIou. fhong fhuong co nhung nghIop vu fhuoc
fruong nao do co cung gIa frI. ví du khI hIon fhI hop dong phaf sInh frong fhang. kof qua so co nhIou
hop dong cua khach hang Iap dI Iap IaI nhu ví du 8-9.
VI du 8-9: SQL dung SKLKCT vo¡ menh de Order by

:-ì-·t Co:tíL, A.coDt
1rc. trìOrc-r:

VoI phaf bIou fron kof qua fra vo nhu sau:

Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ìD 2.'D·.c¯t
ìD 4º.ìtº.ct¯
ìD c.ìD¯.D·2
ìD 2.·ccc·4¯
ìt ìºì.D¯4.º4¯
ìt 2t.DDD
ìt ì.ºt¯.tº2
ìt ·.tDD.DDD
ìt ì'c.¯ì·.º''
ìt 'tì.ºD4.22º
ìt ì4D.ìºD.·4¯
ì2 ì·º
ì2 ìcº.ccc.t·º
ì2 c.c·'.t4¯
ì2 c¯c.ºº¯.¯t¯
ì2 ·'.º¯'.4º'
ì2 ì¯.º24.'·º
ì2 2¯º.cD·.D4º
ì2 c.¯ct.tt¯
ì2 4c'.ìt2
ì· ì·t.¯2¯.t2º
ì· 244.'D4
ì· 2·D.DDD
ì· tD·.D··
ì· ì.4c2.DDD
ì· 4.ttc.ìDD
ì· ì.c·ì.2DD
ì· 4c'.c2c

Trong bao cao chung fa IaI can phaI bIof moI khach hang co bao nhIou Ian fra fIon. fong so fIon
cua moI khach hang da fra Ia bao nhIou7
Ðo Iam dIou nay. chung fa su dung monh do GROUP BY frong phaf bIou SQL dang SELECT
cung voI mof so ham frong MySQL. ban fham khao ví du 8-10 duoc frình bay chI fIof fu ví du 4-8
nhung nhom mau fIn bang monh do Group By.
VI du 8-10: SQL dung SKLKCT vo¡ menh de Group By

:-ì-·t Co:tíL, ·coDt (Co:tíL),
:o.(A.coDt)
Frc. trìOrc-r:
Crcor rv Co:tíL
Orc-r rv Co:tíL

Kof qua fra vo nhu sau:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Co:tíL
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ìt ¯ 2.'ct.ct2.·tº
ì2 ' ·.º4·.D22.tD4
ì· º ì4c.'ì·.·¯º
ìD 4 ¯2.·º2.ºD4

5.3.
5.3.1.
Cuc hum thong dung trong MySQL
Cuc hum trong phut b¡eu GRO¡B BY
¾ Ham AVG: Ham fra vo gIa frI bình quan cua cof hay fruong frong cau fruy van. ví du nhu
phaf bIou sau:

:-ì-·t AVC(A.coDt)
Frc. trìOrc-r:

¾ Ham MIN: Ham fra vo gIa frI nho nhaf cua cof hay fruong frong cau fruy van. ví du nhu
phaf bIou sau:

:-ì-·t \iD(A.coDt)
Frc. trìOrc-r:

¾ Ham MAX: Ham fra vo gIa frI Ion nhaf cua cof hay fruong frong cau fruy van. ví du nhu cac
phaf bIou sau:

:-ì-·t \¬×(A.coDt)
Frc. trìOrc-r:

¾ Ham Count: Ham fra vo so Iuong mau fIn frong cau fruy van fron bang. ví du nhu cac phaf
bIou sau:

:-ì-·t ·coDt(*)
Frc. trìOrc-r:

:-ì-·t ·coDt(Co:tíL)
Frc. trìOrc-r:

:-ì-·t ·coDt(*)
Frc. trìOrc-rL-t¬iì:

¾ Ham Sum: Ham fra vo fong cac gIa frI cua fruong. cof frong cau fruy van. ví du nhu cac phaf
bIou sau:

:-ì-·t :o.(A.coDt)
Frc. trìOrc-r:

Chang han. ban co fho fham khao dIon gIaI foan bo cac ham dung frong monh do GROUP BY.
VI du 8-11: SQL dung SKLKCT vo¡ Group By vu cuc hum

:-ì-·t Co:tíL,
CcoDt (Co:tíL),:o.(A.coDt),
\¬×(A.coDt),
\iD(A.coDt),
Ao(A.coDt)
Frc. trìOrc-r:
Crcor rv Co:tíL
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Orc-r rv Co:tíL

Kof qua fra vo nhu sau:

Co:tíL
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ìt ¯ 2'ctct2·tº ì.'c¯ì·º'' 2tDDD 422·ttDc2
ì2 ' ·º4·D22tD4 ·'º¯'4º' 4c'ìt2 42¯DD2cìì
ì· º ì4c'ì··¯º ì.·t¯2¯t2º 2·DDDD ìº2·'ì¯2.2c
ìD 4 ¯2·º2ºD4 4ºìtºct¯ 2'D·c¯t ìºD'c¯Dì

5.3.2. Cuc hum xu ly chuo¡
¾ Ham ASCII: Ham fra vo gIa frI ma ASCII cua ky fu bon fraI cua chuoI. ví du nhu khaI bao:

:-ì-·t A:Cíí(`TOí`)
Kof qua fra vo nhu sau:

º4
¾ Ham Char: Ham nay chuyon doI kIou ma ASCII fu so nguyon sang dang chuoI:

:-ì-·t ·D¬r(·c)

Kof qua fra vo nhu sau:

=
¾ Ham UPPER: Ham nay chuyon doI chuoI sang kIou chu hoa:

:-ì-·t LJJFF(`KD¬Do`)

Kof qua fra vo nhu sau:

KIANC
¾ Ham LOWER: Ham nay chuyon doI chuoI sang kIou chu fhuong:

:-ì-·t LO'FF(`KD¬Do`)

Kof qua fra vo nhu sau:

×D¬Do
¾ Ham Len: Ham nay fra vo chIou daI cua chuoI:

:-ì-·t ì-D(`í Lc- Yco`)

Kof qua fra vo nhu sau:

ìD
¾ Thu fuc LTRIM: Thu fuc IoaI bo khoang frang bon fraI cua chuoI:

:-ì-·t ìtri.(` KD¬Do`)

Kof qua fra vo nhu sau:

`×D¬Do`
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

¾ Thu fuc RTRIM: Thu fuc IoaI bo khoang frang bon phaI cua chuoI:

:-ì-·t ìtri.(`KD¬Do `)

Kof qua fra vo nhu sau:

`×D¬Do`
¾ Ham Left: Ham fra vo chuoI bon fraI fính fu dau cho don vI frí fhu n:

:-ì-·t ì-1t(`KD¬Do`,·)
Kof qua fra vo nhu sau:

`KD¬`
¾ Ham Right: Ham fra vo chuoI bon phaI fính fu cuoI cho don vI frí fhu n:

:-ì-·t FioDt(`KI¬Do`,4)

Kof qua fra vo nhu sau:

`I¬Do`
¾ Ham Instr: Ham fra vo vI frí chuoI baf dau cua chuoI con frong chuoI xof:

:-ì-·t íN:TF (`KD¬Do`,`JD¬. Ioo KD¬Do`)

Kof qua fra vo nhu sau:

ìì
11 Ia fuong duong vI frí fhu 11 cua chu Khang frong chuoI "Pham Huu Khang"
5.3.3. Cuc hum ve xu ly tho¡ g¡un
¾ Ham CurDate|): Ham fra vo ngay. fhang va nam hIon hanh cua ho fhong:

:-ì-·t ·orc¬t-() ¬: `Tcc¬v i:’

Kof qua fra vo nhu sau

Tcc¬v i:
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
2DDì÷ìì÷2ì

¾ Ham CurTime|): Ham fra vo gIo. phuf va gIay hIon hanh cua ho fhong:

:-ì-·t ·orti.-() ¬: `Ti.- i:’

Kof qua fra vo nhu sau

Ti.- i:
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
D':ì2:Dc

¾ Ham Period_Diff: Ham fra vo so ngay frong khoang fhoI gIan gIua 2 ngay:

:-ì-·t
J-ricc_ci11 (Orc-rL¬t-, o-tc¬t-())
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

¬: `:c Do¬v oio¬ Do¬v tDo ti-D ¹-D Dc. D¬v:`
1rc. trìOrc-r:

Kof qua fra vo nhu sau

:c Do¬v oio¬ Do¬v tDo ti-D ¹-D Dc. D¬v:
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
¯4
¯2

¾ Ham dayofmonth: Ham dayofmonth fra vo ngay fhu may frong fhang:
:-ì-·t c¬vc1.cDtD(·orc¬t-())
¬: `Dc. D¬v Do¬v

Kof qua fra vo nhu sau:


NgoaI cac ham frình bay nhu fron. ban co fho fìm fhay nhIou ham xu Iy vo fhoI gIan frong
phan Funtions xuaf hIon bon phaI man hình cua frình dIou khIon nhu hình 8-6.


Hình 8-6: Su dung chuc nang IuncIfons

5.3.4. Cuc hum ve toun hoc
¾ Ham sqrt: Ham fra vo Ia can baf haI cua mof bIou fhuc:
:-ì-·t :crt (4)

Kof qua fra vo Ia
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

2

¾ Ham Round: Ham fra vo Ia so Iam fron cua mof bIou fhuc:

:-ì-·t rcoDc (¯4º.cº,÷ì)

Kof qua fra vo Ia

¯cDD
Ðo fham khao fhom mof so ham khac ban co fho fham khao frong phan Functions nhu hình
8-9.
5.4. Phut b¡eu SQL dung Select vo¡ AS
KhI can fhIof phaI fhay doI fon fruong nao do frong cau fruy van. ban chI can dung phaf bIou
AS. AS cho phop anh xa fon cu. hay gIa frI chua co fon fhanh fon moI |header).
Ví du. khI su dung GROUP BY o frong phan fron. nhung cof fao ra fu cac phop foan count.
sum. max. min. ... cho ra kof qua khong co header. nghIa Ia khong co fon cof do fham chIou frong khI
goI don chung. Chung fa phaI can phaf bIou AS cho nhung fruong hop nay.
VI du 4-11: SQL dung SKLKCT vo¡ AS vu cuc hum

:-ì-·t Co:tíL,
CcoDt (Co:tíL) ¬: Nc,
:o.(A.coDt) ¬: TíFNIL,
\¬×(A.coDt) ¬: ILLONNIAT,
\iD(A.coDt) ¬: ILNIONIAT,
Ao(A.coDt) ¬: TFLNCFíNI
Frc. trìOrc-r:
Crcor rv Co:tíL
Orc-r rv Co:tíL

Kof qua hIon fhI nhu sau:

Co:tíL Nc TíFNIL ILLONNIAT ILNIONIAT TFLNCFíNI
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ìt ¯ 2'ctct2·tº ì.'c¯ì·º'' 2tDDD 422·ttDc2
ì2 ' ·º4·D22tD4 ·'º¯'4º' 4c'ìt2 42¯DD2cìì
ì· º ì4c'ì··¯º ì.·t¯2¯t2º 2·DDDD ìº2·'ì¯2.2c
ìD 4 ¯2·º2ºD4 4ºìtºct¯ 2'D·c¯t ìºD'c¯Dì

5.5. Phut b¡eu SQL dung Select vo¡ L¡m¡t N , M
Phaf bIou SQL dang SELECT cho phop fruy Iuc chI mof so mau fIn fính fu vI frí fhu n don vI
frí fhu m frong Table |fhoo mof fIou chuan hay sap xop nao do). Ðo Iam dIou nay. frong phaf bIou
SQL dang SELECT ban dung chI dInh fu khoa LIMIT voI so Iuong mau fIn can Iay fu vI frí fhu n
don m.
Chang han. frong fruong hop ban khaI bao Select ` from tblOrders limit 0.10. Kof qua so fra vo
10 mau fIn dau fIon frong bang tblOrders.
Ðan cung co fho su dung kof hop LIMIT voI cac monh do nhu WHERE. ORDER BY nham fao
ra kof qua nhu y muon.
Ðo you cau khac nhau fhong qua phaf bIou SQL dang SELECT co su dung LIMIT, nghIa Ia kof
qua fra vo so Iuong 10 mau fIn dau fIon voI faf ca cac cof frong bang tblOrders
VI du 8-12: Phut b¡eu SQL dung SKLKCT vo¡ L¡m¡t N,M
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


:-ì-·t *
Frc. trìOrc-r:
Li.it D,ìD
Kof qua fra vo nhu sau:

Orc-ríL Orc-rL¬t- Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷
Dì 2DDì÷D'÷Dc ìD 2'D·c¯t
D2 2DDì÷D'÷Dc ìD 4ºìtºct¯
D· 2DDì÷D'÷Dc ìD cìD¯D·2
D4 2DDì÷D'÷Dº ìD 2.·ccc·4¯
Dc 2DDì÷D'÷Dº ìt ì.ºìD¯4º4¯
Dt 2DDì÷D'÷ìD ìt 2tDDD
D¯ 2DDì÷D'÷ìì ìt ìºt¯tº2
Dº 2DDì÷D'÷ì2 ìt ·tDDDDD
D' 2DDì÷D'÷ì· ìt ì.'c¯ì·º''
ìD 2DDì÷D'÷ì4 ìt '.tìºD422º

Nou muon Ioc ra 10 hop dong co so fIon nhIou nhaf. ban chI can su dung sap xop fhoo cof
TotalAmount hay Amount frong bang tblOrders.
VI du 8-13: Phut b¡eu SQL dung SKLKCT vo¡ L¡m¡t N,M

:-ì-·t Orc-ríL,Orc-rL¬t-,Co:tíL,A.coDt
Frc. trìOrc-r:
Orc-r rv A.coDt L-:·
Li.it D,ìD

Kof qua fra vo nhu sau:

Orc-ríL Orc-rL¬t- Co:tíL A.coDt
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷
Dt 2DDì÷D'÷ìD ìt 2tDDD
2t 2DDì÷D'÷ì· ì· 2·DDDD
2c 2DDì÷D'÷ìì ì· 244'D4
2· 2DDì÷D'÷ìì ì2 4c'ìt2
·ì 2DDì÷D'÷ìt ì· 4c'c2c
2¯ 2DDì÷D'÷ì4 ì· tD·D··
2º 2DDì÷D'÷ìc ì· ì4c2DDD
·D 2DDì÷D'÷ìc ì· ìc·ì2DD
D¯ 2DDì÷D'÷ìì ìt ìºt¯tº2
Dì 2DDì÷D'÷Dc ìD 2'D·c¯t

Nou muon Ioc ra 10 san pham co so Iuong ban nhIou nhaf. ban chI can su dung sap xop fhoo
cof so Iuong Qtty.
VI du 8-14: Phut b¡eu SQL dung Select vo¡ L¡m¡t N,M

:-ì-·t ít-.íL,Ottv,Jri·-,A.coDt
1rc. trìOrc-rL-t¬iì:
'D-r- A.coDt>ìD
crc-r rv Ottv
Li.it D,ìD

Kof qua fra vo nhu sau:

ít-.íL Ottv Jri·- A.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì 'DD ì2DDD ì2'tDDDD
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

2 ìDDD ì2DDD ì44DDDDD
· cDDD ì2DDD ¯2DDDDDD
· tDDD ì2DDD ºt4DDDDD
4 ºDDD ì2DDD ìc2DDDDD
4 ºDDD ì2DDD ìc2DDDDD
4 ºDDD ìDDDD ìc2DDDDD
c 'DDD ì2DDD 2'tDDDDD
c 'DDD ì2DDD ì2'tDDDDD
c 'DDD ì2DDD ì2'tDDDDD

5.6. Phut b¡eu SQL dung SKLKCT vo¡ O¡ST¡NCT
Nou co mof hay nhIou bang kof noI voI nhau. so xay ra frung Iap nhIou mau fIn. Nhung frong
fruong hop nay ban chI can Iay ra mof mau fIn frong fap mau fIn frung Iap. ban su dung phaf bIou
SQL dang SELECT voI chI dInh DISTINCT.
VI du 8-14: Phut b¡eu SQL dung SKLKCT

:-ì-·t ít-.íL,Ottv,Jri·-,A.coDt
1rc. trìOrc-rL-t¬iì:
crc-r rv Ottv

Kof qua fra vo nhu sau:

ít-.íL Ottv Jri·- A.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì 'DD ì2DDD ì2'tDDDD
2 ìDDD ì2DDD ì44DDDDD
· cDDD ì2DDD ¯2DDDDDD
· tDDD ì2DDD ºt4DDDDD
4 ºDDD ì2DDD ììc2DDDDD
4 ºDDD ì2DDD ììc2DDDDD
4 ºDDD ìDDDD ììc2DDDDD
c 'DDD ì2DDD ì2'tDDDDD
c 'DDD ì2DDD ì2'tDDDDD
c 'DDD ì2DDD ì2'tDDDDD

...
...

VI du 8-15: Phut b¡eu SQL dung SKLKCT vo¡ O¡ST¡NCT

:-ì-·t Li:tiD·t ít-.íL,Ottv,Jri·-,A.coDt
Frc. trìOrc-rL-t¬iì:
Orc-r rv Ottv

Kof qua IoaI bo nhung mau fIn frung Iap nhu sau:

ít-.íL Ottv Jri·- A.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì 'DD ì2DDD ì2'tDDDD
2 ìDDD ì2DDD ì44DDDDD
· tDDD ì2DDD ºt4DDDDD
4 ºDDD ì2DDD ììc2DDDDD
c 'DDD ì2DDD ì2'tDDDDD
...
...
...

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

5.7.
5.7.1.
Nhup du l¡eu bung phut b¡eu SQL dung ¡nsert
KhI can fhom mau fIn vao bang frong co so du IIou MySQL. ban co nhIou cach do fhuc hIon
cong vIoc nay. Trong Visual Basic 6.0, VB.NET, C Sharp hay Java co nhung phuong fhuc do fhom
mau fIn vao bang frong co so du IIou. Tuy nhIon. do su dung cac phaf bIou SQL mang fính chuyon
nghIop frong MySQL. ban can su dung phaf bIou INSERT.
Ðan co fho su dung phaf bIou Insert ngay fron ung dung kof noI voI MySQL. Trong fruong hop
ban su dung co so du IIou SQL Server hay Oracle. ban co fho fao ra mof Stored Procedure voI muc
dích INSERT du IIou vao bang chI dInh fruoc.
KhI fhom du IIou. can chu y kIou du IIou gIong hoac fuong ung kIou du IIou da khaI bao cua cof
do. nou khong phu hop fhì IoI so phaf sInh.
NgoaI ra ban can quan fam don quyon cua User dang fruy cap co so du IIou. User phaI duoc cap
quyon Insert du IIou vao fung bang cu fho |quyon nay do nha quan frI co so du IIou phan quyon cho
User do).
Trong phaf bIou INSERT INTO chung foI fhuc hIon fron bang tblOrderDetails va bang
tblOrderDetailsHist. haI bang nay co cau fruc nhu sau:

/* Baûng tblOrderDetails*/
CFFATF TAFLF trìcrc-rc-t¬iì: (
ít-.íL iDt(·) oD:ioD-c LFFALLT `D` ,
Orc-ríL iDt(·) oD:ioD-c LFFALLT `D` ,
Nc tiDviDt(·) oD:ioD-c LFFALLT `D` ,
Ottv iDt(·) oD:ioD-c LFFALLT `D` ,
Jri·- iDt(·) oD:ioD-c LFFALLT `D` ,
Li:·coDt iDt(·) oD:ioD-c LFFALLT `D` ,
A.coDt rioiDt(·) oD:ioD-c LFFALLT `D`
);


/* Baûng tblOrderDetailsHist, duøng ñeå chöùa caùc thoâng tin
hôïp ñoàng chi tieát khi hôïp ñoàng cuûa khaùch haøng naøy keát thuùc,
chöông trình töï ñoäng xoaù trong tblOrderDetails vaø lö tröõ laïi
trong baûng tblOrderDetailsHist.*/

CFFATF TAFLF trìcrc-rc-t¬iì:Di:t (
ít-.íL iDt(·) oD:ioD-c LFFALLT `D` ,
Orc-ríL iDt(·) oD:ioD-c LFFALLT `D` ,
Nc tiDviDt(·) oD:ioD-c LFFALLT `D` ,
Ottv iDt(·) oD:ioD-c LFFALLT `D` ,
Jri·- iDt(·) oD:ioD-c LFFALLT `D` ,
Li:·coDt iDt(·) oD:ioD-c LFFALLT `D` ,
A.coDt rioiDt(·) oD:ioD-c LFFALLT `D`
);

KhI Insert du IIou vao bang. co 3 fruong hop xay ra: insert du IIou vao bang fu cac gIa frI cu
fho. insert vao bang Iay gIa frI fu mof hay nhIou bang khac. va cuoI cung Ia kof hop ca haI fruong
hop fron.
¡nsert vuo bung luy g¡u trj cu the:

íN:FFT íNTO <T¬rì-D¬.-><·cìo.DD¬.- ìi:t>`
V¬ìo-: (c¬t¬_¬ìo-)

VI du 8-16: ¡NSKRT du l¡eu vuo bung tu g¡u trj cu the
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

/* Theâm maåu tin vôùi moät soá coät */

íN:FFT íNTO
TFLCL:TO\FF:
(Co:tN¬.-,L:-rD¬.-,J¬::wcrc,
Accr-::,T-ì,F¬×Nc,F.¬iì,CcDt¬·t,
CcoDtrvCcc-,JrciD·-Ccc-)
V¬ìo-: (`KD¬·D :¬D CFNTLFY`, ‘·-Dtorv’,
’ìììì’,’c L- Lci’,’ºt¯t¯t¯’,’º¯t¯t¯t’,
‘·-Dtorvcv¬Dcc.·c.’,’Ic¬Do ADD’,
‘VNA’,’IC\’)

/* Theâm maåu tin vôùi moät soá coät */

íN:FFT íNTO
TFLOFLFF:(Orc-ríL,Orc-rL¬t-,
Co:tíL,L-:·rirticD,A.coDt)
V¬ìo-: (`ìì`,·orc¬t-(),’ì`,
`L¬t D¬Do co¬ .¬Do`, 2DDDD)

5.7.2. ¡nsert vuo bung luy g¡u trj tu bung khuc:

íN:FFT íNTO <T¬rì-D¬.-ì><·cìo.DD¬.- ìi:t>`
:-ì-·t ·cìo.DD¬.- ìi:t`
Frc. <T¬rì-D¬.-2>
'D-r- <CcDciticD:>

VI du 8-1?: ¡NSKRT vuo bung tu g¡u trj cuu bung khuc

/* Theâm maåu tin vôùi caùc coät cuï theå */
/* Chuyeån taát caû nhöõng hôïp ñoàng chi tieát töø baûng
tblOrderDetails vaøo baûng tblOrderDetailsHist */

íN:FFT íNTO
TFLOFLFFLFTAíL:Ií:T(
ít-.íL,
Orc-ríL,
Nc,
Ottv,
Jri·-,
Li:·coDt,
A.coDt)

:FLFCT
ít-.íL,
Orc-ríL,
Nc,
Ottv,
Jri·-,
Li:·coDt,
A.coDt
Frc. trìOrc-rL-t¬iì:
OFLFF FY Orc-ríL A:C

/* Coù theå vieát laïi theâm maåu tin vôùi taát caû caùc coät nhö sau
Chuyeån taát caû nhöõng hôïp ñoàng chi tieát töø baûng tblOrderDetails vaøo baûng tblOrderDetailsHist vôùi ñieàu
kieän soá coät töông öùng trong baûng tblOrderDetails baèng vôùi soá coät trong baûng tblOrderDetailsHist, baïn coù
theå vieát laïi nhö sau */
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


íN:FFT íNTO TFLOFLFFLFTAíL:Ií:T
:FLFCT * 1rc.
trìOrc-rL-t¬iì:
OFLFF FY Orc-ríL A:C

5.7.3. ¡nsert vuo bung luy g¡u trj cu the, bung khuc:

íN:FFT íNTO <T¬rì-D¬.-ì><·cìo.DD¬.- ìi:t>`
:-ì-·t ·cìo.DD¬.- ìi:t`, ¬ìo-:ìi:t
Frc. <T¬rì-D¬.-2>
'D-r- <·cDciticD:>
OFLFF FY <·cìo.D D¬.-> A:CLF:C
VI du 8-18: ¡NSKRT vuo bung tu g¡u trj cu the, bung khuc

/* Theâm maåu tin vôùi caùc coät cuï theå */
/* Chuyeån taát caû nhöõng hôïp ñoàng chi tieát töø baûng tblOrderDetails vaøo baûng tblOrderDetailsHist. Giaû söû
raèng, ngoaøi nhöõng coät gioáng nhö tblOrderDetails, baûng tblOrderDetailsHist coøn coù theâm coät Tranferdate.
*/

íN:FFT íNTO
TFLOFLFF:Ií:T(
Orc-ríL,
Orc-rL¬t-,
F-·-i-Fcìic,
Co:tíL,
L-:·riicD,
A.coDt,
Ii:tcrvc¬t-)

:FLFCT
Orc-ríL,
Orc-rL¬t-,
F-·-i-Fcìic,
Co:tíL,
L-:·riicD,
A.coDt,
o-tc¬t-() ¬: Ii:tcrvc¬t-
Frc. trìOrc-r:
wD-r- \cDtD(Orc-rL¬t-)ì2
Orc-r rv Orc-rL¬t-,Co:tíL

/* Coù theå vieát laïi theâm maåu tin vôùi taát caû caùc coät nhö sau */
/* Chuyeån taát caû nhöõng phieáu thu trong thaùng 12 töø baûng tblOrders vaøo baûng tblOrdersHist vôùi ñieàu kieän
soá coät töông öùng trong baûng tblOrders baèng vôùi soá coät trong baûng tblOrdersHist, baïn coù theå vieát laïi nhö
sau */

íN:FFT íNTO
TFLOFLFFLFTAíL:Ií:T(
ít-.íL,
Orc-ríL,
Nc,
Ottv,
Jri·-,
Li:·coDt,
A.coDt,Tr¬D1-rL¬t-)

:FLFCT
ít-.íL,
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Orc-ríL,
Nc,
Ottv,
Jri·-,
Li:·coDt,
A.coDt,CorL¬t-()
Frc. trìOrc-rL-t¬iì:
OFLFF FY Orc-ríL A:C

5.8. Phut b¡eu SQL dung ¡POATK
Phaf bIou SQL dang UPDATE dung cap nhaf IaI du IIou da fon faI frong bang. KhI UPDATE
dung cap nhaf du IIou cho mof mau fIn chI dInh nao do fhuong UPDATE su dung chung voI monh do
WHERE.
Nou can cap nhaf faf ca cac mau fIn frong bang ban co fho bo monh do WHERE. Phaf bIou nay
co cau fruc nhu sau:

/* neáu caäp nhaát giaù trò cuï theå */
Lrc¬t- <t¬rì- D¬.->
:-t <·cìo.D><¬ìo->,<·cìo.D><¬ìo->`
wD-r- <r-:tri·ti- ·cDciticD:>`

/* neáu caäp nhaát giaù trò laø keát quaû traû veà töø phaùt bieåu
select treân moät hay nhieàu baûng khaùc */

Lrc¬t- <t¬rì- D¬.->
:-t <·cìo.D><:-ì-·t .. 1rc. t¬rì-D¬.- wD-r- ...>
wD-r- <r-:tri·ti- ·cDciticD:>`

UPDATE co fho anh huong don nhIou bang. nhung cap nhaf gIa frI chI co hIou Iuc fron bang
do. ban co fho fham khao phan nay frong chuong ko fIop JOIN TABLE.
Cap nhaf gIa frI cu fho vao mof hay nhIou cof mInh hoa frong ví du 8-18 sau:

VI du 8-18: ¡POATK tren cuc cot du l¡eu tu g¡u trj cu the

/* caäp nhaät coät vôùi giaù trò cuï theå */

Lrc¬t- trìCo:tc.-r:
:-t Co:tN¬.-`CcDo tv TNII Cc·¬ ·cì¬ Vi-tD¬.`
'D-r- Co:tíL‘ì2’

/* caäp nhaät moät coät vôùi giaù trò coät khaùc trong baûng
tblOrderDetails*/

Lrc¬t- trìOrc-r:
:-t A.coDt A.coDt*.Dì,
Tct¬ìA.coDtA.coDt*D.ì
'D-r- \cDtD(Orc-rL¬t-)ì2

/* caäp nhaät moät coät vôùi giaù trò töø baûng khaùc*/
/* caäp nhaät coät Price vôùi giaù trò töø coät Cost cuûa baûng tblItems, khai baùo sau chæ ñuùng trong MySQL 4.1 trôû
veà sau*/


Lrc¬t- trìOrc-rL-t¬iì:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

:-t Jri·-
(:-ì-·t ci:tiD·t Cc:t`
1rc. trìít-.:
wD-r- ít-.íLtrìOrc-rL-t¬iì:.ít-.íL)
'D-r- Jri·-<ìDDD

/* caäp nhaät moät coät vôùi giaù trò cuï theå vôùi ñieàu kieän töø baûng khaùc, , khai baùo sau chæ ñuùng trong MySQL
4.1 trôû veà sau */

Lrc¬t- trìOrc-rL-t¬iì:
:-t Jri·- Jri·-*ìD,
A.coDt Ottv*(Jri·-+ì)
'D-r- ít-.íL iD
(:-ì-·t ci:tiD·t ít-.íL
1rc. trìOrc-rL-t¬iì:
wD-r- Jri·->ìDDD)

5.9. Phut b¡eu SQL dung OKLKTK
VoI phaf bIou SQL dang DELETE fhì don gIan hon. KhI fhuc hIon Ionh xoa mau fIn frong bang
chung fa chI can quan fam don fon bang. va monh do WHERE do xoa voI nhung mau fIn da chon Ioc
nou co. Cu phap cua Delete:

L-ì-t- 1rc. <t¬rì- D¬.->
'D-r- <·cDciticD>
VoI monh do WHERE gIong nhu baf ky monh do WHERE nao frong phaf bIou SELECT hay
UPDATE va INSERT cua baf ky ung dung co so du IIou nao co su dung SQL.
Conditions co fho Ia phop foan gIua cac cof va gIa frI. nhung cung co fho gIa frI Ia kof qua fra
vo fu mof phaf bIou SELECT khac.
ChI chu: Khong co khaI nIom xoa gIa frI frong mof cof. vì xoa gIa frI mof cof dong nghIa voI
cap nhaf cof do bang gIa frI rong.
VI du 8-19: Xou muu t¡n vo¡ phut b¡eu SQL dung OKLKTK

/* Xoaù maåu tin töø baûng vôùi ñieàu kieän */

L-ì-t- 1rc. trìCo:tc.-r:
'D-r- Co:tN¬.- i: Doìì

Trong fruong hop co rang buoc vo quan ho cua du IIou. fhì xoa mau fIn phaI fuan fhu fhoo quy
fac: Xoa mau fIn con fruoc roI moI xoa mau fIn cha.
Chang han. frong fruong hop fa co 2 bang: hop dong ban hang |tblOrders) va hop dong ban
hang chI fIof |tblOrderDetails).
Ðo xoa mof hop dong ban can xoa mau fIn frong bang tblOrders fruoc roI moI don cac mau fIn
frong bang tblOrderDetails.
VI du 8-20: Xou muu t¡n vo¡ Oelete

/* Xoaù maåu tin töø baûng con */
L-ì-t- 1rc. trìOrc-rL-t¬iì:
wD-r- Orc-ríLì2·

/* Xoaù maåu tin töø baûng cha */
L-ì-t- 1rc. trìOrc-r:
wD-r- Orc-ríLì2·
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Ðan co fho fhuc hIon mof phaf bIou SQL dang DELETE voI dIou kIon frong monh do WHERE
Iay gIa frI fra vo fu phaf bIou SELECT fu bang khac. khaI bao nhu vay chI co hIou Iuc frong co so du
IIou MySQL phIon ban 8.1 fro vo sau hay frong co so du IIou SQL Server va Oracle.

VI du 8-21: Xou muu t¡n theo quy tuc co rung buoc quun he

/* Xoaù maåu tin töø baûng vôùi ñieàu kieän laáy giaù trò töø baûng khaùc */
L-ì-t- 1rc. trìOrc-rL-t¬iì:
wD-r- ít-.íL iD
(:-ì-·t ít-.íL
1rc. trìít-.:
wD-r- ít-.N¬.- ìi×- `íT°`)

6. PHAT B¡K¡ SQL OANG JO¡N
NgoaI cac phaf bIou SQL voI 4 dang fron. frong phan ko fIop. chung foI frình bay mof so phaf
bIou SQL dang Select do kof noI du IIou gIua cac bang co quan ho voI nhau. nhung phaf bIou so frình
bay frong chuong 5 nhu:
¾ Khaùi nieäm JOIN
¾ Phaùt bieåu INNER JOIN
¾ Phaùt bieåu LEFTJOIN
¾ Phaùt bieåu RIGHT JOIN
6.1. Khu¡ n¡em ve quun he
Ðo phaf frIon ung dung Web bang baf ky IoaI co so du IIou nao. gIaI doan phan fích fhIof ko ho
fhong cuc ky quan frong. Nou kof qua phan fích khong foI uu fhì ung dung do khong fho daf duoc gIa
frI ky fhuaf cung nhu gIa frI fhuong maI. ThIof ko co so du IIou khong foI uu. chung co fho dan don
vIoc chuong frình chay cham va khong bon vung.
Mof khI ung dung chay cham dI do co so du IIou khong foI uu fhì raf co fho ban phaI fhIof ko
va xay dung IaI fu dau foan bo cau fruc cua chuong frình va co so du IIou.
Xuaf phaf fu Iy do nay. khI xay dung mof ung dung fhong fIn quan Iy. chung fa can phaI qua
nhung buoc phan fích fhIof ko ho fhong ky Iuong do co duoc mo hình quan ho va ERD fruoc khI don
cac mo hình chuc nang chI fIof.
Tuy nhIon. frong Iy fhuyof mof so kIon fhuc co ban baf buoc ban phaI fhuc hIon fhoo mo hình
ho fhong ung voI nhung quan ho foan von. nhung frong fhuc fo. do fính dac fhu cua ung dung. fhuong
ban phaI fhIof ko IaI mo hình fhoo nhu cau can doI gIua do phuc fap va fính foI uu.
Trong ung dung ban hang qua mang Test da frình bay frong chuong 3. khI quan fam don mof
hop dong fron mang. ngoaI nhung fhong fIn IIon Iac vo khach hang. ban can phaI Iuu fru du IIou
khac nhu chIof hang mua. phuong fhuc fra fIon. phuong fhuc gIao hang.... Van do duoc fhao Iuan o
day. moI hop dong co nhì6u maf hang chI fIof.
Trong fruong hop nay. chung fa co 6 fhuc fho IIon quan nhu sau. fhuc fho danh muc Customers
|fhong fIn IIon Iac cua khach hang). Orders |hop dong mua hang). OrderDetails |chI fIof hang mua).
Items |danh muc san pham).



CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM













So do 8-1: Mo hình quan ho
CIa su rang khI nhap so IIou vao co so du IIou. ung voI hop dong co ma 101. cua khach hang co
fon Nguyon Van A. ... co haI san pham chI fIof: 11 |Nuoc ngof) va 32 |xa phong Lux).
Trong fruong hop nay ban dang co mof mau fIn hop dong frong bang tblCustomers, mof mau
fIn hop dong frong bang tblOrders va haI mau fIn frong bang tblOrderDetails.
Nou muon bIof fhong fIn hop dong cua khach hang A. ro rang ban can dung phaf bIou SELECT
voI monh do kof hop fu 3 bang fron. Kof qua fra vo 2 mau fIn Ia su kof hop fhong fIn fu haI bang
tblCustomers. tblOrders va tblOrderDetails.
KhI fhuc fhI phaf bIou SQL dang SELECT ung voI co so du IIou nhu fron ban phaI duyof qua
haI mau fIn.
Taf nhIon. khI vIof ung dung fhì dIou nay chap nhan duoc. va co fho coI Ia foI uu. CIa su rang.
ung dung nay duoc phaf frIon fron WEB can Iuu fam don van do foI uu foc do fruy van fhì sao7
NguoI fhIof ko co so du IIou frong fruong hop nay phaI fhay doI IaI cau fruc do fang foc do fruy
cap qua mang khI xu Iy fron co so du IIou cua nguoI dung.
1 - n
1 - n
1 - n
Ifoms
Ordor
ÐofaIIs
Ordors
Cusfomors
6.2. Khu¡ n¡em ve menh de JO¡N
Trong hau hof phaf bIou SELECT. phan Ion kof qua ma ban mong muon Iay vo dou co IIon
quan don mof hoac nhIou bang khac nhau. Trong fruong hop nhu vay. khI fruy van du IIou ban can su
dung monh do JOIN do kof hop du IIou fron haI hay nhIou bang IaI voI nhau.
KhI su dung JOIN. ban can quan fam don fruong |cof) nao frong bang fhu nhaf co quan ho voI
fruong |cof) nao frong bang fhu haI. Nou mo hình quan ho cua ban khong foI uu hay khong dung.
quan frình su dung JOIN so cho kof qua fra vo khong nhu y muon.
Tro IaI ung dung ban hang qua mang frong gIao frình nay. khI xuaf mof hop dong ban hang
cho khach hang. fhoo fhIof ko frong co so du IIou chung fa co raf nhIou bang IIon quan don nhau.
Chang han. nou quan fam ban hang fhì ban cho aI. Suy ra. IIon quan don fhong fIn khach
hang. ban san pham gì cho ho fhì IIon quan don ma san pham. nou khach hang fra fIon fhì IIon quan
don phIou fhu. nou khach hang co cong no fhì IIon quan don no ky fruoc...
Trong phan nay. chung foI fIop fuc fhIof ko mof so bang du IIou cung voI kIou du IIou fuong ung
va quan ho gIua cac bang duoc mo fa nhu sau:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


trìCo:tc.-r: (c¬DD :¬·D ×D¬·D D¬Do)
Co:tíL` iDt ¬otc_iD·r-.-Dt Jri.¬rv ×-v,
Co:tN¬.-` ¬r·D¬r` (cD) NLLL ,
Accr-::` ¬r·D¬r` (ìDD) NLLL,
T-ì` ¬r·D¬r` (2D) NLLL,
F¬×Nc` ¬r·D¬r` (2D) NLLL,
F.¬iì` ¬r·D¬r` (cD) NLLL,
CcDt¬·t` ¬r·D¬r` (cD) NLLL
CcoDtrv` ¬r·D¬r` (·) NLLL,
JrciD·-` ¬r·D¬r` (·) NLLL
fbIOrdors |Hop dong ban hang)
Orc-ríL` iDt` Nct Doìì
¬otc_iD·r-.-Dt Jri.¬rv K-v,
Orc-rL¬t-` c¬t-` NLLL ,
Co:tíL` iDt ,
L-:·rirticD` ¬r·D¬r` (2DD) NLLL ,
:DirCc:t` 1ìc¬t` NLLL ,
Tr¬DíL` tiDviDt` NLLL ,
J¬v.-DtíL` tiDviDt` NLLL ,
A.coDt` 1ìc¬t` NLLL ,
Tct¬ìA.coDt` 1ìc¬t` NLLL ,
fbIOrdorÐofaIIs |Hop dong ban hang chI fIof)
:oríL` iDt` ¬otc_iD·r-.-Dt NOT NLLL ,
Orc-ríL` iDt ,
ít-.íL` iDt,
Nc` iDt,
Ottv` iDt` NLLL ,
Jri·-` iDt NLLL ,
Li:·coDt` Fìc¬t` NLLL ,
A.coDt` Fìc¬t` NLLL
fbIIfoms |Ðanh sach san pham)
ít-.íL` iDt ¬otc_iD·r-.-Dt Jri.¬rv ×-v,
ít-.N¬.-` ¬r·D¬r` (2DD) NLLL ,
LDit` D¬r·D¬r` (2D) NLLL ,
Cc:t` Fìc¬t` NLLL ,
A·ti-` tiDviDt` NOT NLLL ,
C¬t-ocrv` iDt

Ðan co fho fìm fhay cac bang du IIou con IaI frong du IIou Test frong dIa dính kom fhoo sach.
6.3. Menh de ¡NNKR JO¡N
Phaf bIou SQL dang SELECT co su dung monh do INNER JOIN fhuong dung do kof hop haI
hay nhIou bang du IIou IaI voI nhau. cu phap cua SELECT co su dung monh do INNER JOIN:

:FLFCT :FLFCT Lí:T`
FFO\ <FíF:T_TAFLFNA\F>
íNNFF .OíN <:FCONL_TAFLFNA\F>
ON <.OíN CONLíTíON>
'IFFF <CFíTFFíAN:>
OFLFF FY <COLL\N Lí:T>
A:C LF:C`

Nou ban can Iay ra mof so cof frong cac bang co kof noI IaI voI nhau bang monh do INNER
JOIN fhì cu phap nay vIof IaI nhu sau:

:FLFCT FíFLLì,FíFLL2, ...`
FFO\ <FíF:T_TAFLFNA\F>
íNNFF .OíN <:FCONL_TAFLFNA\F>
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

ON <.OíN CONLíTíON>
'IFFF <CFíTFFíAN:>
OFLFF FY <COLL\N Lí:T>
A:C LF:C`

VI du 8-23: ¡NNKR JO¡N vo¡ mot so cot ch¡ djnh
/* in ra danh saùch khaùch haøng mua haøng trong thaùng 10 */

:-ì-·t Co:tN¬.-,Orc-ríL,
Orc-rL¬t-,A.coDt,
Tct“A.coDt
1rc. trìCo:tc.-r:
iDD-r ¸ciD trìOrc-r:
cD trìCo:tc.-r:.Co:tíL trìOrc-r:.Co:tíL
wD-r- .cDtD (Orc-rL¬t-) ìD
crc-r rv Co:tN¬.-

Kof qua fra vo nhu sau:

Co:tN¬.- Orc-ríL Orc-rL¬t- .. Tct¬ìA.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
CFNTLFY Ict-ì ì· 2DDì÷ìD÷ì¯ ·ºººDDDDD
CFNTLFY Ict-ì ì4 2DDì÷ìD÷ìº cìº4DDDDD
CFNTLFY Ict-ì ìt 2DDì÷ìD÷ì¯ ·ºººDDDDD
CFNTLFY Ict-ì ì¯ 2DDì÷ìD÷ìº ì44DDDDD
CFNTLFY Ict-ì ìº 2DDì÷ìD÷ìº ì2'tDDDD
CFNTLFY Ict-ì ììD 2DDì÷ìD÷ìº 2ìtDDDDDD
Jì¬:¬ Ict-ì ì2 2DDì÷ìD÷ì¯ 4D·2DDDDD
Jì¬:¬ Ict-ì ì' 2DDì÷ìD÷ì¯ ºt4DDDDD
Jì¬:¬ Ict-ì ìì 2DDì÷ìD÷ì¯ c¯tDDDDDD
Jì¬:¬ Ict-ì ìc 2DDì÷ìD÷ì¯ 2ººDDDDDD

Nou ban can Iay ra faf ca cac cof frong cac bang co kof noI IaI voI nhau bang monh do INNER
JOIN, cu phap fron co fho vIof IaI nhu sau:

:FLFCT 1ir:t_t¬rì-D¬.-.*,
:-·cDc_t¬rì-D¬.-.*
,D-×t t¬rì- D¬.-`
FFO\ <1ir:t_t¬rì-D¬.->
íNNFF .OíN <:-·cDc_t¬rì-D¬.->
ON <¸ciD ·cDciticD:>
íNNFF .OíN <D-×t_t¬rì-D¬.->
ON <¸ciD ·cDciticD:>`
'IFFF <·cDciticD:>
OFLFF FY <·cìo.D ìi:t>
A:C LF:C`

VI du 8-24: ¡NNKR JO¡N vo¡ tut cuc truong l¡en quun
/* in ra danh saùch khaùch haøng mua haøng trong thaùng 10 */

:-ì-·t Co:tíL,Co:tN¬.-,Orc-ríL,
Orc-rL¬t-,Tct¬ìA.coDt
1rc. trìCo:tc.-r:
iDD-r ¸ciD trìOrc-r:
OD TrìCo:tc.-r:.Co:tíLtrìOrc-r:.Co:tíL
wD-r- .cDtD (Orc-rL¬t-) ìD
crc-r rv Co:tN¬.- LF:C

Kof qua fra vo nhu sau:
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


Co:tíL Co:tN¬.- .. Orc-ríL ..Tct¬ìA.coDt
÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì· Jì¬:¬ Ict-ì .. ìì .. c¯tDDDDDD
ì· Jì¬:¬ Ict-ì .. ìc .. 2ººDDDDDD
ì2 Jì¬:¬ Ict-ì .. ì2 . . 4D·2DDDDD
ì2 Jì¬:¬ Ict-ì .. ì' .. ºt4DDDDD
ìt CFNTLFY Ict-ì .. ì· .. ·ºººDDDDD
ìt CFNTLFY Ict-ì .. ì4 .. cìº4DDDDD
ìt CFNTLFY Ict-ì .. ìt .. ·ºººDDDDD
ìt CFNTLFY Ict-ì .. ì¯ .. ì44DDDDD
ìt CFNTLFY Ict-ì .. ìº .. ì2'tDDDD
ìt CFNTLFY Ict-ì .. ììD .. 2ìtDDDDDD

Nou frong nhung bang can kof noI co fon fruong |cof) gIong nhau fhì khI fhuc fhI phaf bIou
SQL dang SELECT phaI chI ro cof fhuoc bang nao. Trong fruong hop ca haI cung Iay du IIou ra fhì
ban can chuyon anh xa fon khac cho cof fhong qua monh do AS. ví du nhu:

:FLFCT 1ir:t_t¬rì-D¬.-.Co:tíL ¬: CL:TíL,
:-·cDc_t¬rì-D¬.-.Co:tíL ¬: CL:TíL
FFO\ <1ir:t_t¬rì-D¬.->
íNNFF .OíN <:-·cDc_t¬rì-D¬.->
ON <¸ciD ·cDciticD>
'IFFF <·rit-ri¬D:>
OFLFF FY <·cìo.D ìi:t>
A:C LF:C`

Nou frong nhung bang can kof noI do co fon fruong |cof) gIong nhau va khong duoc chI ro nhu
fruong hop fron khI khaI bao frong co so du IIou SQL Server. khI fhuc fhI phaf bIou SQL dang
SELECT ban so bI IoI. chang han nhu:

:FLFCT 1ir:t_t¬rì-D¬.-.*, :-·cDc_t¬rì-D¬.-.*
FFO\ <1ir:t_t¬rì-D¬.->
íNNFF .OíN <:-·cDc_t¬rì-D¬.->
ON <¸ciD ·cDciticD>
'IFFF <·rit-ri¬D:>
OFLFF FY <·cìo.D ìi:t>
A:C LF:C`

Sorvor: Msg 209. IocoI 16. Sfafo IIno 1
AmbIguous coIumn namo 'CusfIÐ'

Tuy nhIon. voI phaf bIou fron ban co fho fhuc fhI frong co so du IIou MySQL. NgoaI ra. phaf
bIou SQL dang SELECT su dung INNER JOIN ban co fho anh xa |alias) fon cua bang fhanh fon
ngan gon do do fham chIou vo sau.
Thuc ra phaf bIou ALIAS co y nghIa gIong nhu AS voI fon cof frong bang fhanh fon cof khac
frong phaf bIou SELECT.

:-ì-·t r.*,:.*
1rc. t¬rì-D¬.-ì
iDD-r ¸ciD t¬rì-D¬.-2
OD t¬rì-D¬.-ì.1i-ìcì t¬rì-D¬.-2.1i-ìc2

VI du 8-25: ¡NNKR JO¡N vo¡ unh xu ten bung
/* in ra danh saùch khaùch haøng mua haøng trong thaùng 10 */
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


:-ì-·t ·.Co:tN¬.-,
:.Orc-ríL,:.Orc-rL¬t-,
:.Tct“A.coDt
1rc. trìCo:tc.-r ·
iDD-r ¸ciD trìOrc-r: :
OD ·.Co:tíL:.Co:tíL
wD-r- .cDtD (:.Orc-rL¬t-) ìD
crc-r rv ·.Co:tN¬.- LF:C

Kof qua fra vo nhu sau:

Co:tN¬.- Orc-ríL Orc-rL¬t- .. Tct¬ìA.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷CFNTLFY Ict-ì ì· 2DDì÷
ìD÷ì¯ .. ·ºººDDDDD
CFNTLFY Ict-ì ì4 2DDì÷ìD÷ìº .. cìº4DDDDD
CFNTLFY Ict-ì ìt 2DDì÷ìD÷ì¯ .. ·ºººDDDDD
CFNTLFY Ict-ì ì¯ 2DDì÷ìD÷ìº .. ì44DDDDD
CFNTLFY Ict-ì ìº 2DDì÷ìD÷ìº .. ì2'tDDDD
CFNTLFY Ict-ì ìì 2DDì÷ìD÷ìº .. 2ìtDDDDDD
Jì¬:¬ Ict-ì ì2 2DDì÷ìD÷ì¯ .. 4D·2DDDDD
Jì¬:¬ Ict-ì ì' 2DDì÷ìD÷ì¯ .. ºt4DDDDD
Jì¬:¬ Ict-ì ìì 2DDì÷ìD÷ì¯ .. c¯tDDDDDD
Jì¬:¬ Ict-ì ìc 2DDì÷ìD÷ì¯ .. 2ººDDDDDD

Taf nhIon. ban cung co fho vIof phaf bIou fron ung voI fung cof muon Iay ra bang cach khaI bao
fon cof.
6.4. Menh de LeIt Jo¡n
Truong hop ban mong muon kof qua Iay ra frong haI bang kof hop nhau fhoo dIou kIon: Nhung
mau fIn bang bon fraI fon faI ung voI nhung mau fIn o bang bon phaI khong fon faI ban hay dung
monh do LEFT JOIN frong phaf bIou SQL dang SELECT, cu phap co dang:

:-ì-·t <Ccìo.D ìi:t>
1rc. ì-1tt¬rì-D¬.-
LFFT .OíN rioDtt¬rì-D¬.-
cD ì-1tt¬r×-D¬.-.1i-ìcìrioDtt¬rì-D¬.-.1i-ìc2
'D-r- <·cDciticD:>
Orc-r rv <·cìo.D D¬.->
A:CLF:C

Chang han. ban chon ra faf ca cac san pham |voI cac cof) co hay khong co doanh so ban frong
fhang hIon faI. Mof so san pham khong ban frong fhang so co cof Amount co cof Amount gIa frI
NULL.

VI du 8-26: SKLKCT dung LKFT JO¡N
/* in ra danh saùch saûn phaåm baùn trong thaùng 10 */

:-ì-·t ít-.íL,ít-.N¬.-,A.coDt
1rc. trìít-.:
ì-1t ¸ciD trìOrc-rL-t¬iì:
cD trìít-.:.ít-.íLtrìOrc-rL-t¬iì:.ít-.íL
crc-r rv A.coDt
Kof qua fra vo nhu sau:

ít-.íL ít-.N¬.- A.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì2 A:'÷tDVJ NLLL
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

ì· A:'÷tDVT NLLL
ì4 A:'÷ttDT ì2DV T' 2'·4D NLLL
ì4 A:'÷tºcV ì2DV T' 2'44D NLLL
ìc A:'tDVJ 22DV ·4c¯ì NLLL
ìt A:'÷4c7ìTì 2'tDDDD
ì¯ A:'÷4cYìT ì2¯V ì44DDDDD
ìº A:'÷4cYìT 22DV ¯2DDDDDD
ì' A:'÷4cYìT 22DV ºt4DDDDD
2D A:'÷4c7ìT ìc2DDDDD
...
6.5. Menh de R¡ght Jo¡n
Nguoc IaI voI phaf bIou SQL dang SELECT su dung monh do LEFT JOIN Ia phaf bIou SQL
dang SELECT su dung monh do RIGHT JOIN so xuaf du IIou cua bang bon phaI cho du du IIou cua
bang bon fraI khong fon faI. cu phap co dang:

:-ì-·t <Ccìo.D ìi:t>
Frc. ì-1tt¬rì-D¬.-
FíCIT .OíN rioDtt¬rì-D¬.-
OD ì-1tt¬r×-D¬.-.1i-ìcìrioDtt¬rì-D¬.-.1i-ìc2
'D-r- <·cDciticD:>
Orc-r rv <·cìo.D D¬.->
A:CLF:C

Trong ví du sau. ban co fho chon ra faf ca cac san pham co hay khong co doanh so ban frong
fhang hIon faI. Cac san pham khong fon faI doanh so ban so khong hIon ra.
VI du 8-2?: SKLKCT dung R¡GHT JO¡N
/* in ra danh saùch saûn phaåm baùn trong thaùng ngaøy 17 */
/* trong phaùt bieåu SELECT naøy coù söû duïng meänh ñeà
WHERE söû duïng phaùt bieåu SELECT khaùc, keát quaû cuûa SELECT trong meänh ñeà WHERE traû veà moät maûng
OrderID */

:-ì-·t ít-.N¬.-,Ottv,
Jri·-,A.coDt
Frc. trìít-.:
FioDt ¸ciD trìOrc-rL-t¬iì:
OD trìít-.:.ít-.íLtrìOrc-rL-t¬iì:.ít-.íL
'D-r- Orc-ríL iD (ì2,ì4,2·,ìc)

Orc-r rv ít-.íL

Kof qua fra vo nhu sau:

ít-.N¬.- Ottv Jri·- A.coDt
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
A:'÷4cYìT ì2¯V :LíA2'·cD ììDDD ì2DDD cº4DDDDD
A:'÷4cYìT ì2¯V :LíA2'·cD ìDDDD ì2DDD 44DDDDDD
A:'÷4cYìT ì2¯V :LíA 2'·cD ìDDDD ì2DDD ì44DDDDD
A:'÷4cYìT ì2¯V :LíA 2'·cD ìDDDD ì2DDD 44DDDDDD
A:'÷4cYìT ì2¯V :LíA 2'·cD ììDDD ì2DDD cº4DDDDD
A:'÷4cYìT ì2¯V :LíA 2'·cD ìDDDD ì2DDD 44DDDDDD
A:'÷4cYìT ì2¯V :LíA 2'·cD ììDDD ì2DDD cº4DDDDD
A:'÷4cYìT 22DV AFC 2'·'ì tDDD ì2DDD ºt4DDDDD
A:'÷4c7ìT 'DDD ì2DDD 2'tDDDDD
A:'÷4c7ìT 'DDD ì2DDD 2'tDDDDD
...

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

6.6. Phep toun hop (un¡on)
Union khong gIong nhu nhung monh do JOIN da gIoI fhIou fron day. Union Ia phop foan dung
do noI haI hay nhIou cau fruy van dang Select IaI voI nhau.
ÐoI voI JOIN. ban co fho kof noI du IIou duoc fhuc hIon fhoo chIou ngang. ÐoI voI Union ban
kof noI du IIou duoc fhuc hIon fhoo chIou doc.
Ðo chon ra nhung khach hang fhuong xuyon frong tblCustomers. kof qua fra vo Ia danh sach
cac khach hang fhuong xuyon.
VI du 8-28: Khuch hung thuong xuyen trong tblCustomers

:-ì-·t Co:tíL,Co:tN¬.-
1rc. trìCo:tc.-r:

Kof qua fra vo nhu sau:


Co:tíL Co:tN¬.-
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
ì· N-w 'crìc Ict-ì
ì2 KiDD Lc Ict-ì
ìt CFNTLFY Ict-ì
ìD JLA7A Ict-ì

Ðo chon ra nhung khach hang vang IaI frong tblTempCustomers. kof qua fra vo Ia danh sach
cac khach hang vang IaI.
VI du 8-29: Khuch hung vung lu¡ trong tblTempCustomers

:-ì-·t Co:tíL,Co:tN¬.-
1rc. trìT-.rCo:tc.-r:

Kof qua fra vo nhu sau:


Co:tíL Co:tN¬.-
÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
2· CcDo tv Doc· oi¬i ×D¬t ‘ì2’COLA
24 CcDo tv Doc· oi¬i ×D¬t JFJ:í
2c CcDo tv Doc· oi¬i ×D¬t FFLFLLK
2t CcDo tv Doc· oi¬i ×D¬t TFíFíCO

Nou dung phop foan UNION do kof noI haI bang fron. kof qua fra vo Ia danh sach ca haI IoaI
khach hang frong cung mof recordset.
VI du 8-30: SKLKCT su dung phep hop ¡N¡ON

:-ì-·t Co:tíL,Co:tN¬.-
Frc. trìCo:tc.-r:

LNíON

:-ì-·t Co:tíL,Co:tN¬.-
Frc. trìT-.rCo:tc.-r:

Kof qua fra vo nhu sau:

Co:tíL Co:tN¬.-
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

÷÷÷÷÷÷÷÷÷÷ ÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
2· CcDo tv Doc· oi¬i ×D¬t ‘ì2’COLA
24 CcDo tv Doc· oi¬i ×D¬t JFJ:í
2c CcDo tv Doc· oi¬i ×D¬t FFLFLLK
2t CcDo tv Doc· oi¬i ×D¬t TFíFíCO
ì2 KiDD Lc Ict-ì
ìD JLA7A Ict-ì
ìt CFNTLFY Ict-ì
ì· N-w 'crìc Ict-ì

Gh¡ chu: KhI su dung phop foan Union frong phaf bIou SQL dang Select. ban can Iuu y cac quy
dInh sau:
¾ Taf ca nhung fruy van frong UNION phaI cung so cof hay fruong. Nou fruy van fhu nhaf co
haI cof fhì fruy van fhu haI duoc su dung UNION cung phaI co haI cof fuong fu.
¾ KhI su dung UNION. nhung cof nao co fon cof hay bí danh |aIIas) moI fhì kof qua fra vo so
co fua do |header) cua fung cof va fon Ia fon cof cua fruy van fhu nhaf.
¾ KIou du IIou frong cac cof cua fruy van 2 fuong fhích voI kIou du IIou cac cof fuong ung frong
fruy van fhu nhaf.
¾ Trong UNION ban co fho kof hop nhIou cau fruy van IaI voI nhau.
¾ Kof qua hIon ra fhoo fhu fu cua fruy van fu duoI Ion fron.
6.7.
6.7.1.
6.7.2.
SQL dung thuy do¡ vu djnh nghIu co so du l¡eu
Phut b¡eu SQL dung CRKATK
Phaf bIou SQL dang CREATE dung do fao co so du IIou va nhung doI fuong cua co so du IIou
frong MySQL. SQL Server. Oracle. .... chung cu phap nhu sau:

CFFATF L¬t¬r¬:- <L¬t¬r¬:- NA\F>

CFFATF <OF.FCT TYJF>
<OF.FCT NA\F>

¾ OBJECT TYPE: IoaI doI fuong cua co so du IIou ví du nhu Procedure. Table. View....
¾ OBJECT NAME: Ton cua doI fuong frong co so du IIou SQL nhu sp_IC. tblEmployer. ...

Tuo co so du l¡eu - Creute dutubuse
KhI xay dung co so du IIou. ban baf dau fu mo hình co so du IIou ERD. hay fu mof gIaI doan
nao do frong quy frình phan fích fhIof ko ho fhong. Ðo fao co so du IIou fron MySQL hay SQL Server
ban su dung cu phap sau:

CFFATF LATAFA:F <L¬t¬r¬:- D¬.->

Cu phap day du cua phaf bIou fao co so du IIou nhu sau. nou ban su dung co so du IIou SQL
Server:

CFFATF LATAFA:F <c¬t¬r¬:-_D¬.->
ON JFí\AFY` (
N¬.- <`Lcoi·¬ì 1iì- D¬.-`>,` Fiì-N¬.-<`Fiì- N¬.-`>
, :í7F<:i:- iD \-o¬rvt- cr KiìcFvt-> `
, \AX:í7F<:i:- iD \-o¬rvt- cr KiìcFvt-> `, FíLFCFO'TI <Nc c1
Kvìcrvt-J-r·-Dt¬o->`
)`
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM


LOC ON
(
N¬.- <`Lcoi·¬ì 1iì- D¬.-`>,` Fiì-N¬.-<`Fiì- N¬.-`>
, :í7F<:i:- iD \-o¬rvt- cr KiìcFvt-> `
, \AX:í7F<:i:- iD \-o¬rvt- cr KiìcFvt-> `, FíLFCFO'TI <Nc c1
Kvìcrvt-J-r·-Dt¬o->`
)`
COLLATF <Ccìì¬ticD N¬.->`
Fcr Lc¬c Fcr Att¬·D`

6.7.3. O¡en g¡u¡ CRKATK Outubuse trong SQL Server
¾ ON: Ðung do dInh nghIa noI chua co so du IIou va khong gIan chua fap fIn log.
¾ NAME: Ðung dInh nghIa fon cua co so du IIou. Ton nay dung fham chIou khI goI don co so
du IIou. fon duoc dung cho qua frình backup, export, Import, Shrink co so du IIou do.
¾ FILENAME: Ton fap fIn co so du IIou Iuu frong dIa cung. fhong fhuong khI caI SQL Server
Ion o dIa nao fhì gIa frI mac dInh cho phop Iuu fap fIn don fhu muc do. Tuy nhIon. nou
muon ban cung co fho fhay doI vI frí cac file nay.
KhI fao co so du IIou. ban da dInh nghIa vI frí daf fap fIn o fhu muc nao fhì khong fho dI
chuyon mof cach fhu cong |nhu dung Explorer cua Windows). vì Iam dIou do fhaf nguy hIom nhaf Ia
khI du IIou frong co so du IIou dang co gIa frI kInh fo.
¾ SIZE: Ðung Iuong cua co so du IIou khI khoI fao chung. Thong fhuong gIa frI mac dInh Ia 1
MB.
¾ Ðung Iuong phaI Ia so nguyon. co fho fang fhom bang cach su dung fhu fuc Shrink frong
SQL Server.
¾ MAXSIZE: Ðung Iuong Ion nhaf. khI dung Iuong co so du IIou fang Ion don muc MaxSize fhì
dung IaI.
Nou khI dung Iuong bang MaxSize. cac chuyon fac co fho bI huy bo hay fra vo IoI khong fho
fhuc hIon duoc. va co fho Iam cho co so du IIou cua ban bI froo.
Ðo franh dIou nay xay ra. fhì nguoI quan frI co so du IIou phaI fhuong xuyon fhoo gIoI qua frình
fang dung Iuong co so du IIou fhoo fhoI gIan. do co bIon phap franh moI ruI ro co fho xay ra.
¾ FILEGROWTH: Ðung Iuong khoI fao cung dung Iuong foI da cho phop fang frong qua frình
fhom du IIou vao co so du IIou. Nham fu dong hoa. chung fa phaI fhIof Iap qua frình fang fu
dong fhoo chI so KB cho fruoc hay fy Io phan fram fhoo dung Iuong dang co.
¾ LOG ON: Log on cho phop ban quan Iy nhung chuyon fac xay ra frong qua frình su dung co
so du IIou cua SQL Server.
Xuy dung co so du l¡eu Test
Nhu da frình bay o fron. sau day ví du fao co so du IIou Test co cu phap nhu sau:
VI du 8-31: Tuo co so du l¡eu Test trong SQL Server

L:F .¬:t-r

CO

CFFATF LATAFA:F T-:t
ON
( NA\F T-:t,
FíLFNA\F `·:`.::cì¯`c¬t¬`T-:tc¬t..c1`,
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

:í7F ìD,
\AX:í7F cD,
FíLFCFO'TI c )

LOC ON
( NA\F `T-:tìco`,
FíLFNA\F `·:`.::cì¯`c¬t¬`T-:tìco.ìc1`,
:í7F c\F,
\AX:í7F 2c\F,
FíLFCFO'TI c\F )
CO

Ðo don gIan hoa cac doI fuong Table frong co so du IIou Test. chung foI chI frình bay mof vaI
phaf bIou SQL dang Create Table. cac Table khac ban co fho fìm fhay frong co so du IIou dính kom.
VI du 8-32: Tuo mot so bung trong Test

/* Taïo baûng danh saùch khaùch haøng thöôøng xuyeân */

CFFATF TAFLF trì·o:tc.-r: (
Co:tíL iDt(·) oD:ioD-c NOT NLLL ¬otc_iD·r-.-Dt,
L:-rD¬.- ¬r·D¬r(2D) NOT NLLL LFFALLT `` ,
J¬::wcrc ¬r·D¬r(ìD) NOT NLLL LFFALLT `` ,
Co:tN¬.- ¬r·D¬r(cD) ,
Accr-:: ¬r·D¬r(ìDD) ,
T-ì ¬r·D¬r(2D) ,
F¬×Nc ¬r·D¬r(ìD) ,
F.¬iì ¬r·D¬r(cD) ,
CcDt¬·t ¬r·D¬r(cD) ,
CcoDtrvCcc- ·D¬r(·) ,
JrciD·-Ccc- ·D¬r(·) ,
JFí\AFY KFY (Co:tíL),
íNLFX Co:tíL (Co:tíL)
);

/* Taïo baûng hôïp ñoàng mua haøng qua maïng */

CFFATF TAFLF trìcrc-r: (
Orc-ríL iDt(·) NOT NLLL ¬otc_iD·r-.-Dt,
Orc-rL¬t- c¬t- ,
Co:tíL iDt(ìì) ,
L-:·rirticD ¬r·D¬r(ìDD) LFFALLT `D` ,
Tr¬DíL tiDviDt(·) LFFALLT `D` ,
J¬v.-DtíL tiDviDt(·) LFFALLT `D` ,
A.coDt 1ìc¬t LFFALLT `D` ,
:DirCc:t 1ìc¬t LFFALLT `D` ,
Tct¬ìA.coDt 1ìc¬t LFFALLT `D` ,
JFí\AFY KFY (Orc-ríL),
íNLFX Orc-ríL (Orc-ríL)
);

/* Taïo baûng hôïp ñoàng chi tieát mua haøng qua maïng */

CFFATF TAFLF trìcrc-rc-t¬iì: (
ít-.íL iDt(·) oD:ioD-c LFFALLT `D` ,
Orc-ríL iDt(·) oD:ioD-c LFFALLT `D` ,
Nc tiDviDt(·) oD:ioD-c LFFALLT `D` ,
Ottv iDt(·) oD:ioD-c LFFALLT `D` ,
Jri·- iDt(·) oD:ioD-c LFFALLT `D` ,
Li:·coDt iDt(·) oD:ioD-c LFFALLT `D` ,
A.coDt rioiDt(·) oD:ioD-c LFFALLT `D`
);
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mot so quy djnh kh¡ th¡et ke Tuble
6.7.4.
6.7.5.
6.7.6.
Ten cot - Column Nume
Ðaf fon cof cung gIong nhu daf fon bang. co raf nhIou quy fac daf fon |nhu da frình bay o fron
phan table). nhung khuyon khích ban non fhoo mof so quy fac co ban sau:
¾ Ton cof baf dau chu hoa. con IaI bang chu fhuong.
¾ Ton ngan gon va day du y nghIa.
¾ Khong non daf fon cof co khoang frang. sau nay ban so gap nhung phIon foaI khI fham
chIou don cof do.
¾ Khong daf fon cof frung voI nhung fu khoa. fu danh rIong. va nhung ky fu dac bIof nhu
nhung phop foan hay foan fu khac.
¾ Chu y. non daf fon cof cung fon nhung cof co quan ho voI nhung bang khac frong cung co so
du IIou. gIup do hIou va franh bI nham Ian.
Mof so nguoI fhích fhom vao dau gach chan |_) do phan bIof y nghIa hay fon goI cua cof. dIou
nay Ia fuy vao so fhích cua ban. Tuy nhIon chung foI khong fhích quI fac nay.
Nhung doI voI kInh nghIom Iap fhIof ko xay dung co so du IIou fhì ban khong non dung dau
gach duoI _. va dI nhIon frong nhIou fruong hop khac ban so cam fhay kho chIu khI fhom mof dau _
frong fon cua doI fuong cua co so du IIou.
Mac du khong co van do gì cho cu phap hay cac phaf bIou fham chIou don chung. nhung ban so
fhay faI sao chung fa khong non dung dau gach chan |_) khI daf fon doI fuong hay fon co so du IIou
frong MySQL.
¾ Nou ban daf fon co dau _ .ban phaI fon fhoI gIan hay nang Iuong cho hanh dong fao ra dau _
¾ Trong chung muc hay gIoI han nao do do hIou ung cua Font chu co fho phaf sInh IoI so gay
ra nham Ian cho nguoI Iap frình.
¾ NoI fom IaI Ia ban so maf fhom fhoI gIan Iuu fam don chung.
K¡eu du l¡eu - Outu type
Nhu da frình bay cac IaI du IIou frong phan fron. khI xay dung co so du IIou. faf ca nhung
fruong frong bang can phaI co kIou du IIou cu fho. Van do quan frong Ia chon kIou du IIou nao cho
phu hop voI du IIou ma nguoI dung so nhap vao.
Ðo fhIof ko du IIou phu hop voI fhuc fo. ngoaI fính ung dung hop voI ngu canh ban cung can
quan fam don kIou du IIou fuong fhích va chIou daI cua fung cof. Chang han nhu:

Co:tíL` ¬r·D¬r` (ìD)
/* hay */
Co:tíL` iDt

G¡u trj muc djnh - OeIuult
Thong fhuong khI fao ra mof cof frong bang doI khI chung fa can ap dung gIa frI mac dInh.
khong chI cho fruong hop so IIou khong nhap fu bon ngoaI ma con cho cac cof fu dong co gIa frI fu
sInh. VoI nhung Iy do nhu vay. chung fa can co mof so gIa frI mac dInh cho nhung cof can fhIof. ví du
:
¾ Nou cof do Ia so chung fa co gIa frI mac dInh Ia 0
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

¾ Nou cof do Ia ngay fhang chung fa co gIa frI mac dInh Ia ngay nao do |nhu 0000-00-00 Ia
CurDate|))
¾ Nou cof do co gIa frI Ia 0 hoac 1. ban co fho khaI bao gIa frI mac dInh Ia 0 hoac 1
¾ Nou cof do Ia chuoI chung fa co gIa frI mac dInh nhu Ia 'A'
6.7.7. So tu dong uuto_¡ncrement
aufo_Incromonf Ia khaI nIom cuc ky quan frong frong MySQL |fuong duong voI Identity frong
SQL Server, Autonumber frong MS Access). KhI ban muon mof cof co gIa frI fang fu dong nhu
AutoNumber/Identity. ban non dInh nghIa cof do nhu auto_increment..
KhI su dung auto_increment Iam so fang fu dong fhì kIou du IIou Ia so nguyon hoac so nguyon
Ion.
Trong fruong hop. ban khaI bao so fu dong frong SQL Server. ban can phaI khaI bao fhom cac
fhong so nhu seed. Seed Ia gIa frI khoI dau khI SQL Server fu dong fang gIa frI. Increament Ia buoc
fang. no cho bIof moI Ian fang can bao nhIou gIa frI.
Vì du khI fao auto_increment cho cof ItemID [Int] auto_increment. nghIa Ia baf dau so 1 va moI
Ian fang 1 so. Kof qua ban so co Ia 1.2.3.4. ...n.
Trong phaf bIou SQL cua MySQL. do fao bang co ga frI fang fu dong ban chI can khaI bao fon
cof. kIou du IIou Int (Integer) va auto_increment nhu sau:

íLNO íDt ¬otc_iD·r-.-Dt NOT NLLL
Trong gIao dIon do hoa ban chI can check vao fuy chon AutoIncreament nhu hình 8-10.


Hình 8-10: Chon aufo_Incromonf
CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

N¡LL / NOT N¡LL
Ðay Ia frang fhaI cua mof cof frong bang cho phop chap nhan gIa frI NULL hay khong7 Nou
ban chI ra rang buoc gIa frI NOT NULL fhì baf buoc phaI co gIa frI frong cof nay moI khI mau fIn
duoc nhap vao.
ÐoI voI mof so kIou du IIou khong cho phop NULL ban non fhIof Iap gIa frI mac dInh cho cof
do. ví du nhu kIou du IIou bIf khong cho phop NULL.
Trong phaf bIou SQI fao bang. ban chI can khaI bao NULL hay NOT NULL sau kIou du IIou
cua cof do. Trong gIao dIon do hoa chI can danh dau chon vao fuy chon Not NULL nhu hình 8-10.

6.8. Thuy cuu truc do¡ tuong bung ALTKR
KhI chung fa can fhIof phaI sua doI mof phan cau fruc cua cac doI fuong nhu table (view. hay
SP frong SQL Server) vì muc dích nao do. fhì Ðan su dung phaf bIou ALTER do fhay doI cau fruc cua
doI fuong hIon co:

ALTFF <Or¸-·t tvr->
<Or¸-·t N¬.->

KhI mof bang fon faI frong co so du IIou. do nhu cau can fhIof phaI fhay doI cau fruc bang. ban
su dung phaf bIou ALTER TABLE cung cac fham so cua chung nhu cu phap sau:

ALTFF TAFLF t¬rì- ¬ìt-r¬ticD ,¬ìt-r¬ticD`

Chang han. ban co fho su dung phaf bIou ALTER TABLE do fhom mof cof fon AcfIvafo voI
kIou du IIou TInyInf co gIa frI mac dInh Ia 1.
VI du 8-33: Them mot cot ten Act¡vute vuo bung tblOrders

ALTFF TAFLF trìcrc-r:
ALL A·ti¬t- TíNYíNT LFFALLT `ì`

KhI fhay doI fhIof Iap gIa frI mac dInh cho cof ban non quan fam don gIa frI mac dInh do co
phu hop cho nhung mau fIn dang fon faI hay khong.
Muon fhay doI gIa frI mac dInh cua cof cho nhung mau fIn dang fon faI. ban su dung don monh
do phu nhu frong ví du sau:

VI du 8-34: Th¡et lup g¡u trj muc djnh trong bung tblOrders

ALTFF TAFLF trìcrc-r:
CIANCF Orc-rL¬t-
Orc-rL¬t- LATFTí\F
LFFALLT `DDDD÷DD÷DD`

Thay doI kIou du IIou fu Date dang DateTime. ban co fho khaI bao nhu ví du 4-35 sau:

VI du 8-35: Thuy do¡ k¡eu du l¡eu

ALTFF TAFLF trìcrc-r:
CIANCF Orc-rL¬t-
Orc-rL¬t- LATF
LFFALLT `DDDD÷DD÷DD DD:DD:DD`

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Mac khac. ban cung co fho fao hay fhay doI bang frong man hình MySQL-Front. ChI can chon
ngan Database 1 R-Click 1 Create New Table. cua so xuaf hIon nhu hình 8-11.

Hình 8-11: CIao dIon fao bang bang MySQI-Ironf
6.9. Phut b¡eu SQL dung OROP
Ðrop Ia phaf bIou fhuc hIon phop xoa. DROP dung do xoa doI fuong cua co so du IIou nhu bang.
co so du IIou. ...Cu phap cua phaf bIou DROP:

LFOJ <Or¸-·t tvr-> <Or¸-·t D¬.-> , .... D`

Ðan co fho xoa co so du IIou. bang cach khaI bao nhu sau:

Lrcr L¬t¬r¬:- T-:t

/* Phaùt bieåu DROP TABLE chæ roõ baûng naøo caàn xoaù,
neáu xoaù nhieàu baûng thì baïn caàn duøng daáu phaåy (,) */

LFOJ TAFLF trìCo:tc.-r:, trì:orrìi-r:

NgoaI ra. ban cung co fho dung MySQl-Front do xoa bang hay cac doI fuong Table frong co so
du IIou chI dInh. Nou chon nhIou bang cung mof Iuc ban su dung phím Control hay Shift nhu sau:

CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Hình 8-12: Chon doI fuong do xoa bang frong MySQI-Ironf
7. TAO K¡CH BAN SQL- SQL SCR¡PTS
Thong fhuong khI xay dung co so du IIou do phaf frIon ung dung. doI khI ban can chuyon co so
du IIou fu may nay sang may khac. hay fu khu vuc nay hay don khu vuc khac.
Co raf nhIou cach do Iam dIou nay. o day chung foI gIoI fhIou don cac ban mof cong cu faI fao
IaI co so du IIou moI fu kIch ban cua co so du IIou goc.
KIch ban SQL |SQL Script) Ia fong hop faf ca cac phaf bIou SQL dung do fao ra co so du IIou
frong qua frình xay dung chung. chung Iuu fru duoI dang van ban co fon mo rong .sql |cautruc.sql).
Cong cu nay fao kIch ban cho faf ca cac doI fuong cua co so du IIou voI nhung fhuoc fính can
ban. Tuy nhIon. nou ban chon vao fuy chon Data. SQL Script bao gom cac phaf bIou SQL dang Insert
cung voI du IIou frong bang.
Truoc fIon ban co fho nhan fhay cua so cong cu nay frong MySQI-Ironf. bang cach chon fon co
so du IIou Test. sau do chon Tools / Im-Export / Export Table. cua so xuaf hIon nhu hình 8-13 sau:


CIao vIon: Pham Huu Khang
COMP¡TKR LKARN¡NG CKNTKR WWW.H¡¡KHANG.COM

Hình 8-13: Tao kIch ban frong MySQI-Ironf

KKT CH¡ONG
Trong chuong nay. chung foI da gIoI fhIou voI ban hau hof cac phaf bIou SQL fhuoc IoaI dInh
nghIa co so du IIou. fhao fac du IIou nhu Select. Insert. Delete va Update.
Phaf bIou SQL dang Select voI cac monh do nhu JOIN cung phop foan gIua haI hay nhIou bang
frong phaf bIou SQL dang SELECT.
NgoaI ra. chung foI cung frình bay haI IoaI phaf bIou SQL dang dInh nghIa va fhay do co so du
IIou fao nhu CREATE va ALTER, DROP.


CIao vIon: Pham Huu Khang
PHP VA OATABASK 9-1
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
Mon hoc: PHP

BA¡ 9: PHP VA OATABASK

Ðo kof noI co so du IIou mySQI frong PHP. chung fa co nhIou cach
ung voI nhIou phuong fhuc kof noI co so du IIou. frong phan nay chung fa
fap frung fìm hIou cach kof noI co so du IIou mySQI fu PHP bang chính
goI cua no.

Nhung van do chính so duoc do cap frong baI hoc nay
9 KhaI bao kof noI co so du IIou
9 Thom mau fIn
9 Cap nhaf mau fIn.
9 Xoa mau fIn
9 Truy van du IIou

1. KKT NO¡ CO SO O¡ L¡K¡
Ðo kof noI co so du IIou mySQI ban su dung khaI bap nhu sau:

<?php
$link = mysql_connect ("localhost", "root", "")
or die ("Could not connect to MySQL Database");
mysql_select_db("TestDB", $link);
?>


Trong do khaI bao sau Ia kof noI co so du IIou mySQI voI fon sorvor/Ip cung voI usornamo va
passvord:

mysql_connect ("localhost", "root", "")

Va mysqI_soIocf_db|"TosfÐÐ". $IInk); do chon fon co so du IIou sau khI mo kof noI co so du IIou. nou
bIon $IInk co gIa frI Ia faIso fhì kof noI co so du IIou khong fhanh cong.
Sau khI mo kof noI co so du IIou ma khong su dung fìh ban co fho dong kof noI co so du IIou voI cu
phap nhu sau:

mysql_close($link);

Chang han. ban khaI bao frang connocfIon.php do kof noI co so du IIou va dong kof noI ngay
sau khI mo fhanh cong.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
Mo va dong ket noi CSDL MySQL
<?php
$link = mysql_connect ("localhost", "root", "")
or die ("Could not connect to MySQL Database");
mysql_select_db("InterShop", $link);
mysql_close($link);
PHP VA OATABASK 9-2
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
?>
</BODY>
</HTML>

2. THKM MA¡ T¡N
Ðo fhom mau fIn. ban su dung ham mysqI_quory|chuoI Insorf). Chang han. chung fa khaI bao
frang Insorf.php do fhom mau fIn vao bang fbIshIps co haI cof du IIou Ia ShIpIÐ va ShIpNamo nhu ví
du frong frang Insorf.php.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Them mau tin</h3>
<?php

require("dbcon.php");
$sql="insert into tblships values('A01','Testing')";
$result = mysql_query($sql,$link);
$affectrow=0;
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin them vao<?= $affectrow?>
</BODY>
</HTML>

Trong do. ban su dung ham mysqI_quory voI haI fham so Ia $sqI va $IInk. Kof qua fra vo Ia so
mau fIn fhuc fhI. NgoaI ra. ban co fho su dung doan kof noI co so du IIou frong fap fIn dbcon.php nhu
ví du sau:

<?php
$link = mysql_connect ("localhost", "root", "")
or die ("Could not connect to MySQL Database");
mysql_select_db("Test", $link);
?>

Trong fruong hop cho phop nguoI su dung fhom mau fIn fhì ban fhIof ko form you cau nguoI su
dung nhap haI gIa frI sau do submIf don frang ko fIop do fhuc fhI vIoc fhom gf sau do submIf don
frang ko fIop do fhuc fhI vIoc fhom gIa frI vua nhap vao co so du IIou nhu hình 9-1.


PHP VA OATABASK 9-3
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM

Hình 9-1: Thom mau fIn
Ðo Iam dIou nay. fruoc fIon ban khaI bao frang fhom.php. frong do khaI bao doan javascrIpf do
kIom fra du IIou nhap nhu sau:

<SCRIPT language=JavaScript>
function checkInput()
{

if (document.frmPHP.txtID.value=="")
{
alert("Invalid ID, Please enter ID");
document.frmPHP.txtID.focus();
return false;
}

if (document.frmPHP.txtName.value=="")
{
alert("Please enter Name");
document.frmPHP.txtName.focus();
return false;
}
return true;
}
</script>

Ko don khaI bao fho form va haI fho Inpuf IaI foxf you cau nguoI su dung nnap IÐ va Namo nhu
sau:

<form name="frmPHP" method="post"
action="doinsert.php"
onsubmit="return checkInput();">
<tr>
<td align="left" class="content-sm"><b>
Please enter ID and Name
</b></td>
</tr>
<tr>
<td align="left" >ID:</td>
</tr>
<tr>
<td align="left">
<input type="text" name="txtID"
size="25" maxlength="3" class="textbox">
</td>
PHP VA OATABASK 9-4
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
</tr>
<tr>
<td align="left" >Name:</td>
</tr>
<tr>
<td align="left" >
<input type="text" name="txtName"
size="25" maxlength="50" class="textbox">
</td>
</tr>
<tr>
<td align="left" valign="top"> <br>
<input type="submit"
value="Submit" class="button">
<input type="reset" value="Reset" class="button">
</td>
</tr>
</form>

Iuu y rang. ban khaI bao so ky fu Ion nhaf cho phop nhap bang voI kích fhuoc da khaI bao
frong co so du IIou ung voI fhuoc fính maxIongfh.
KhI nguoI su dung nhap haI gIa frI va nhan nuf submIf. frang ko fIop duoc frIou goI. Trang nay
Iay gIa frI nhap bang cach su dung bIon form hay $HTTP_POST_VARS. ÐoI voI fruong hop nay
chung fa su dung bIon form nhu frang doInsorf.php.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Them mau tin</h3>
<?php
$affectrow=0;
require("dbcon.php");
$sql="insert into tblships(ShipID,ShipName) ";
$sql .=" values('".$txtID."','".$txtName."')";
$result = mysql_query($sql,$link);
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin them vao<?= $affectrow?>
</BODY>
</HTML>

3. CAP NHAT MA¡ T¡N
ÐoI voI fruong hop cap nhaf mau fIn. ban cung su dung ham mysqI_quory voI phaf bIou !pdafo
fhay ví Insorf nhu fron. ví du chung fa khaI bao frang updafo.php do cap nhaf mau fIn frong bang
fbIShIps voI fon Ia !pdafoTosfIng khI ma co gIa frI Ia A01.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Cap nhat mau tin</h3>
<?php

require("dbcon.php");
$sql="Update tblships set ShipName='UpdateTesting' ";
$sql.=" where ShipID='A01'";
$result = mysql_query($sql,$link);
$affectrow=0;
if($result)
PHP VA OATABASK 9-5
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin cap nhat <?= $affectrow?>
</BODY>
</HTML>

Iuu y rang. do bIof so mau fIn da fhuc fhI boI phaf bIou SQI ban su dung ham
mysqI_affocfod_rovs.

if($result)
$affectrow=mysql_affected_rows();


Tuong fu nhu fron. ban co fho fhIof ko form cho phop nguoI su dung cap nhaf du IIou bang cach
fhIof ko form you cau nguoI su dung nhap ma va fon cap nhan.
Truoc fIon fhIof ko from cho phop nhap du IIou do cap nhaf nhu ví du frang capnhaf.php. sau
khI hoc phan fruy van xong. fhaqy vì nhap ma ban cho phop nguoI su dung chon frong danh sach da
co nhu hình 9-2.


Hình 9-2: Cap nhaf du IIou
Sau khI nguoI su dung nhan nuf submIf. frang doupdafo.php so frIou goI. kof qua fra vo 1 hay 0
mau fIn.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Cap nhat mau tin</h3>
<?php
$affectrow=0;
require("dbcon.php");
$sql="update tblships set ShipName='";
$sql .=$txtName."' where ShipID='".$txtID."'";
$result = mysql_query($sql,$link);
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
PHP VA OATABASK 9-6
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
So mau tin cap nhat <?= $affectrow?>
</BODY>
</HTML>

4. XOA MA¡ T¡N
Tuong fu nhu vay khI xoa mau fIn. ban chI fhay doI phaf bIou SQI dang ÐoIofo nhu ví du frong
fap fIn doIofo.php.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Xoa mau tin</h3>
<?php

require("dbcon.php");
$sql="Delete From tblships where ShipID='A01'";
$result = mysql_query($sql,$link);
$affectrow=0;
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin da xoa <?= $affectrow?>
</BODY>
</HTML>

ÐoI voI fruong hop xoa fhì don gIan hon. ban cIh can bIof duoc ma can xoa. chính vì vay frong
fruong hop nay chung fa chI can fhIof ko frang cho phop nhap ma nhu hình 9-3.



Hình 9-3: Xoa 1 mau fIn
Sau khI nhap ma can xoa. nou nguoI su dung nhan nuf ÐoIofo Iap fuc frang dodoIofo.php so
frIou goI va xoa mau fIn fuong ung.

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
PHP VA OATABASK 9-?
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
<BODY>
<h3>Xoa mau tin</h3>
<?php
$affectrow=0;
require("dbcon.php");
$sql="delete from tblships ";
$sql .=" where ShipID='".$txtID."'";
$result = mysql_query($sql,$link);
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin xoa <?= $affectrow?>
</BODY>
</HTML>

5. TR¡Y VAN O¡ L¡K¡
Ðo fruy van du IIou ban su dung ham mysqI_num_rovs do bIof duoc so mau fIn fra vo va ham
mysqI_fofch_array do doc fung mau fIn va mang sau do frình bay gIa frI fu mang nay. Chang han.
chung fa fao mof fap fIn IIofko.php dung do IIof ko danh sach mau fIn frong bang fbIShIps nhu hình
9-4.


Hình 9-4: IIof ko mau fIn
Ðo Iam dIou nay. ban khaI bao doan chuong frình doc bang du IIou fuong fu nhu ví du sau:

<?php
require("dbcon.php");
$totalRows = 0;
$stSQL ="select * from tblShips";
$result = mysql_query($stSQL, $link);
$totalRows=mysql_num_rows($result);
?>

Sau do. dung ham mysqI_fofch_array do doc fung mau fIn va In ra nhu sau:

<?php
if($totalRows>0)
{
$i=0;
while ($row = mysql_fetch_array ($result))
{
$i+=1;
PHP VA OATABASK 9-8
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
?>
<tr valign="top">
<td>
<?=$row["ShipID"]?> </td>
<td ><?=$row["ShipName"]?></td>
</tr>

Trong fruong hop so mau fIn fra vo Ia 0 fhì In ra cau fhong bao khong fìm fhay nhu sau:

<?php
}
}else{
?>
<tr valign="top">
<td >&nbsp;</td>
<td > <b><font face="Arial" color="#FF0000">
Oop! Ship not found!</font></b></td>
</tr>
<?php
}
?>

6. KKT L¡AN
Trong baI nay. chung fa fap frung fìm hIou cach kof noI co so du IIou. fhom. xoa cap nhaf va
IIof ko mau fIn. Trong baI ko fIop chung fa fìm hIou nhIou cac frình bay du IIou. xoa mau fIn fhoo
dang mang.


PHP VA OATABASK 9-1
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
Mon hoc: PHP

BA¡ 10: XOA, CAP NHAT O¡ L¡K¡ OANG MANG

Trong baI fruoc chung fa da Iam quon voI cach xoa mau fIn frong co
so du IIou mySQI. ÐoI voI fruong hop xoa mof Iuc nhIou mau fIn. chung fa
phaI xay dung frang PHP co su dung fho Inpuf dang chockbox.
Nhung van do chính so duoc do cap frong baI hoc nay
9 IIof ko du IIou dang danh sach
9 Xoa nhIou mau fIn
9 Cap nhaf nhIou mau fIn

1. L¡KT KK O¡ L¡K¡
Ðo xoa nhIou mau fIn cung mof Iuc. fruoc fIon ban khaI bao frang PHP do IIof ko danh sach mau
fIn frong mang du IIou chang han. moI mau fIn xuaf hIon mof chockbox fuong ung. Chockbox nay
co gIa frI Ia ma nhan dang cua mau fIn do. Trong fruong hop nay chung fa dung cof khoa cua
ma chuyon hang |SubCafoIÐ) frong bang fbISubCafogorIos dInh nghIa frong frang IIofko.php nhu
hình 10-1.



PHP VA OATABASK 9-2
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
Hình 10-1: IIof ko danh sach IaI san pham
Ðo cho phop Iay duoc nhIou gIa frI chon cua san pham nhu hình fron. ban khaI bao cac chockbox
nay cung fon |gIa su fon Ia chkId) va gIa frI frI Ia SubCafoIÐ cua moI san pham nhu ví du 10-1
frong frang IIofko.php.

<?php
if($totalRows>0)
{
$i=0;
while ($row = mysql_fetch_array ($result))
{
$i+=1;
?>
<tr valign="top">
<td><input type=checkbox name=chkid
value="<?=$row["SubCateID"]?>"> </td>
<td><?=$row["CateID"]?> </td>
<td><?=$row["SubCateID"]?> </td>
<td ><a href="capnhat.php?id=<?=$row["SubCateID"]?>">
<?=$row["SubCateName"]?></a></td>

</tr>
<?php
}
?>
<tr valign="top">
<td colspan="4" align="middle">
<hr noshade size="1">
</td>
</tr>
<tr valign="top">
<td colspan=3><input type=submit value="Delete">
<input type=hidden name=from_ value="subcategories">
<input type=hidden name=type value="0">
<input type=hidden name=chon value="">
<input type=button value="New"
onclick="window.open('them.php',target='_main')"></td>
<td >Tong so mau tin <?=$i?></td>
</tr>
<?php
}else{
?>
<tr valign="top">
<td >&nbsp;</td><td >&nbsp;</td><td >&nbsp;</td>
<td > <b><font face="Arial" color="#FF0000">
Oop! Ship not found!</font></b></td>
</tr>
<?php
}
?>
Trong do. haI khaI bao sau:

<input type=hidden name=from_ value="subcategories">
<input type=hidden name=type value="0">
<input type=hidden name=chon value="">

Cho bIof ban submIf fu frang nao va IoaI xoa nhIou mau fIn hay mof mau fIn doI voI bang fuong
ung. Muc dích cua van do nay Ia frang doIofo su dung chung cho nhIou bang khac nhau va fu frang
IIof ko |xoa nhIou) hoac fu frang odIf |1 mau fIn cu fho).
NgoaI ra. chung fa khaI bao <Inpuf fypo=hIddon namo=chon vaIuo=""> do nhan gIa frI chon fron
cach chockbox bang cach khaI bao doan javascrIpf nhu sau:

<script>
function calculatechon()
PHP VA OATABASK 9-3
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
{
var strchon="";
var alen=document.frmList.elements.length;
var buttons=1;

alen=(alen>buttons)?document.frmList.chkid.length:0;
if (alen>0)
{
for(var i=0;i<alen;i++)
if(document.frmList.chkid[i].checked==true)
strchon+=document.frmList.chkid[i].value+",";
}else
{
if(document.frmList.chkid.checked==true)
strchon=document.frmList.chkid.value;
}
document.frmList.chon.value=strchon;
return isok();
}

</script>

Tuy nhIon. do nhIou IoaI san pham fhuoc cac nhom san pham khac nhau. chính vì vay ban khaI bao
danh sach nhom san pham fron fho soIocf cho phop nguoI su dung IIof ko sach fhoo nhom san
pham nhu hình 10-2.



Hình 10-2: IIof ko danh sach IoaI sach
Ðo IIof ko danh sach nhom frong bang fbICafogorIos. bang cach khaI bao phuong fhuc nhan chuoI
SQI dang SoIocf va gIa frI mac dInh fra vo nhIou phan fu fho opfIon frong fap fIn dafabaso.php
nhu ví du 10-2.

PHP VA OATABASK 9-4
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
function optionselected($stSQL,$item,$links)
{
$results = mysql_query($stSQL, $links);
$totalRows=mysql_num_rows($results);
$strOption="<option value=\"\" selected>";
$strOption .="--Select--</option>";
if($totalRows>0)
{
while ($row = mysql_fetch_array ($results))
{
$strOption .="<option value=\"" ;
$strOption .=$row["ID"]."\"";
if($row["ID"]==$item)
$strOption .=" selected ";
$strOption .= ">".$row["Name"];
$strOption .="</option>";
}
}
return $strOption;
}

Sau do. goI phuong fhuc nay frong frang IIofkofhoonhom.php nhu ví du 10-3.

<?php
require("dbcon.php");
require("database.php");
$id="";
if (isset($cateid))
$id=$cateid;
$stSQL ="select CateID As ID, CateName as Name from tblCategories ";
$result = mysql_query($stSQL, $link);
$totalRows=mysql_num_rows($result);
$strOption=optionselected($stSQL,$id,$link);
?>
<form name=frmMain method=post>
<tr>
<td align=left colspan=4>
Category: <select name=cateid onchange="document.frmMain.submit();">

<?=$strOption?>
</select></td>
<td align=right>&nbsp;</td>
</tr>
</form>

Ian dau fIon ban co fho chon mac dInh mof nhom hoac IIof ko faf ca. khI nguoI su dung chon nhom
san pham nao do fhì frang IIofkofhoonhom.php so IIof ko danh sach IoaI sach cua nhom sach do.
Ðo Iam dIou nay. ban khaI bao fho form voI fho soIocf nhu ví du 10-4.

<form name=frmMain method=post>
<tr>
<td align=left colspan=4>
Category: <select name=cateid onchange="document.frmMain.submit();">

<?=$strOption?>
</select></td>
<td align=right>&nbsp;</td>
</tr>
</form>

KhI nguoI su dung chon cac mau fIn nhu hình 10-2 va nhan nuf ÐoIofo. dua vao gIa frI cua nuf co
fon acfIon |frong fruong hop nay Ia ÐoIofo). ban co fho khaI bao bIon do Iay gIa frI chon bang cach
khaI bao nhu ví du 10-5.

$strid=$chon;
PHP VA OATABASK 9-5
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
$strid=str_replace(",","','",$strid);
Ðua vao fho hIddon khaI bao frong cac frang frình bay danh sach |chang an IIofkofhoonhom.php)
mau fIn nhu sau:

<input name="from" type=hidden value="subcategories">

Ðan co fho bIof fu frang nao goI don frang dodoIofo.php do quay fro vo khI fhuc hIon xong fac vu xu
Iy.
NgoaI ra. dua vao gIa frI cua nuf acfIon do fhuc hIon phaf bIou SQI. Chang han. frong fruong hop
nay nou nguoI su dung nhan huf ÐoIofo fhì ban khaI bao nhu ví du 10-6 sau:

switch($strfrom)
{
case "subcategories":
$stSQL ="delete from tblsubcategories where SubCateID in('".$strid."')";
$strlocation="Location:lietketheonhom.php";
break;
case "categories":
$stSQL ="delete from tblcategories where CateID in('".$strid."')";
$strlocation="Location:nhom.php";
break;
}

Sau do. ban co fho fhuc fhI phaf bIou SQI vua khaI bao o fron nhu ví du 10-7.

if($stSQL!="")
{
$result = mysql_query($stSQL, $link);
}
Iuu y rang. ban cung non khaI bao fry cafch frong khI Iam vIoc voI co so du IIou. NgoaI ra. ban
cung phaI xac nhan fruoc khI fhuc fhI hanh dong xoa mau fIn chon bang cach khaI bao doan
JavascrIpf nhu sau:

<script>
function isok()
{
return confirm('Are you sure to delete?');
}
</script>

Sau do goI frong bIon co onsubmIf cua form nhu sau:

<form action=dosql.php method=post onsubmit="return calculatechon();">

2. CAP NHAT NH¡K¡ MA¡ T¡N
Tuong fu nhu fruong hop ÐoIofo. khI ban duyof |approvaI) mof so mau fIn fhoo mof cof du IIou nao
do. chang han. frong fruong hop nay chung fa cho phop su dung nhung san pham da qua su dong y
cua nha quan Iy fhì cof du IIou AcfIvafo cua bang fbIfoms co gIa frI Ia 1.
Ðo Iam dIou nay. fruoc fIon ban IIof ko danh sach san pham nhu hình 10-3.
PHP VA OATABASK 9-6
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM



Hình 10-3: IIof ko danh sach san pham duyof hay chua
Tuong fu nhu frong fruong hop doIofo. ban khaI bao frang do!pdafo nhu sau:

<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Cap nhat mau tin</h3>
<?php
require("dbcon.php");
$strid=$chon;
$strid=str_replace(",","','",$strid);
$strfrom="";
if(isset($from_))
{
$strfrom=$HTTP_POST_VARS{"from_"};
}
$strtype="";
if(isset($type))
{
$strtype=$HTTP_POST_VARS{"type"};
}

$stSQL="";
if($strfrom<>"")
{
switch($strfrom)
{
case "items":
$stSQL ="update tblItems set Activate=1 where ItemID
in('".$strid."')";
PHP VA OATABASK 9-?
COMP!TÐR IÐARNINC CÐNTÐR WWW.H!!KHANC.COM
break;
}
if($stSQL!="")
{
$result = mysql_query($stSQL, $link);
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
}
}
?>
So mau tin cap nhat <?= $affectrow?>
</BODY>
</HTML>

3. KKT L¡AN
Trong baI nay. chung fa fìm hIou chuc nang xoa. cap nhaf nhIou mau fIn bang cach su dung fho
Inpuf IoaI chockbox cung fon va khac gIa frI. baI ko fIop chung fa fIop fuc fìm hIou vo chuc nang
dang nhap frong PHP.


Sign up to vote on this title
UsefulNot useful