PHP02 - Tong Phuoc Khai

Click here to load reader

  • date post

    11-Apr-2015
  • Category

    Documents

  • view

    59
  • download

    0

Embed Size (px)

Transcript of PHP02 - Tong Phuoc Khai

LAP TRNH WEB NG VI

PHP / MySQLGUESTBOOK CATALOG FORUM SHOPPING CART

PHAN 2Tong Phc Khai (tong hp& bien dch)

Phng phap truy xuat CSDL

MySQL

1- T Command Prompt 2- Bang lenh PHP 3- Dung phpMyadmin

e hieu c chng nay mot cach ro rang, trc tien ban can phai co mot so kien thc c ban ve C S D Lieu quan he. Neu ban a hoc qua mot khoa can ban ve MS Access trong chng trnh ao tao chng ch B chang han th han nhien ban co the tiep tuc. Con neu nh ban cha biet g ve no th toi se ban en no trong phan Phu Luc cua giao trnh nay hoac ban co the tm ngay mot tai lieu tham khao ve CSDL, de nhat la tai lieu va MS Access ... ... Toi chac rang bay gi ban a co kien thc ve CSDL va hieu biet Table la g roi! Co hang khoi cong viec ban se phai lam viec oi vi cac Table va ban se c hng dan can ke trong quyen sach nay. Ban se phai vt qua mot so kien thc ve no e mi co the thanh thao trong thao tac vi Table. Nh ban biet ay: Con ng i en thanh cong khong co trai tham san au! Neu ban a tng lam viec vi MS SQL Server hay Access chung eu co ho tr viec tao CSDL rat la de dang vi giao dien trc quan. oi vi MySQL ban cung co the s dung cong cu trc quan o la phpMyadmin. Tuy nhien, ban phai hoc cach thao tac vi CSDL bang dong lenh, toi chac rang ieu nay se rat co ch cho ban. Muon chng trnh cua ban trong luc chay thao tac t ong vi CSDL th ban can hang ta lenh PHP/SQL e thc hien cac yeu cau cua chng trnh.

Trc khi chung ta tao cac table trong CSDL cua MySQL, co mot vai th ban can phai hieu ro. Nhng khai niem c ban ma toi sap gii thieu sau ay rat quan trong. Ban hay chac rang mnh a nam ky ve chung trc khi thc hien viec thiet ke d lieu.

Null

Viec au tien ban phai lam trong viec thiet ke mot table la quyet nh xem mot field co cho phep gia tr NULL hay khong. Trong CSDL quan he, gia tr NULL cua mot field ong ngha vi no co the chap nhan khong co d lieu trong o. Nen nh rang gia tr NULL khac vi gia tr cua mot chuoi khong co ky t trong o hoac so co gia tr 0. oi khi trong chng trnh, ban se thc hien mot so ong tac so sanh xem mot chuoi nao o co cha gia tr hay khong, no co the la mot cau lenh IF. Xet mot v du PHP nh sau:$var //this is a variable used in the test if ($var == ) { echo Var is an empty string; } else { echo $var; }

Neu ban thc hien viec so sanh xem gia tr so co phai 0 hay khong th cung thc hien tng t. ong tac so sanh tren se khong lam viec c oi vi gia tr NULL. Ban nen hieu rang NULL la khong cha bat ky gia tr g trong o, cho nen viec so sanh tr khong mang y ngha g ca. Trong chng 3 ban se thay rang oi vi gia tr NULL oi hoi lap trnh vien phai rat can nhac khi viet lenh lien ket table.Trong lenh SELECT cua SQL, co mot so cach e ban co the kiem tra neu nh mot field cha gia tr NULL. Trc het ban hay s dung ham Isnull(). Gia s tm mot record trong table ma gia tr midle_name la NULL, ban co the s dung query sau: select * from names where isnull(middle_name); Hoac lay cac record ma middle_name khac NULL:select * from names where !isnull(middle_name); select * from users were addr2 is null; select * from users where addr2 is not null;

Ban cung co the s dung is null va is not null:

e ro hn, ban hay xem chuyen g xay ra khi toi co gang lien ket hai table sau:

Khach_hang ten Trung Khai

ho_lot Le Anh Tong Phuoc

ba_xa 1321 Null

Hon_nhan ba_xa 1321

ten Diem

ho_lot Nguyen Thuy

Neu ban muon tm ten cac khach hang va ten nhng ba xa cua ho, ban se phai lien ket 2 table nay thong qua field ba_xa. (Xin ban ch lo lang khi cha hieu ve cu phap, ban se hoc ngay phan tiep theo thoi).SELECT * FROM khach_hang, hon_nhan WHERE khach_hang.ba_xa = hon_nhan.ba_xa

Viec thc hien nay ch ung oi vi Trung, nhng se co van e oi vi Khai bi v anh ta hay con oc than va ba_xa cua anh ta la NULL. Trong chng 3 ban se khao sat ky hn ve van e nay.

Index

Ngi ta noi rang u iem vt troi cua He quan tr CSDL quan he la no thc hien cac viec tm kiem hay sap xep nhng khoi lng d lieu khong lo mot cach rat nhanh chong. S d no thc hien c viec nay la do no co cha mot c cau lu tr d lieu goi la INDEX. INDEX cho phep database server tao c mot field ac trng tm kiem vi toc o kho ng. Cac INDEX ac biet ho tr mot hoac mot nhom cac record trong mot table cha so lng ln cac record. Chung cung ho tr toc o cho cac ham lien ket hoac tach nhom d lieu nh min(), max(), ban se tm hieu Chng 3. Vi cac tnh nang vt troi nay, tai sao ngi ta lai khong tao index trong tat ca cac field cua mot table? Co mot so ieu tr ngai nh sau: Th nhat, index co se lam cham mot so tien trnh trong CSDL. Moi lan bao tr cac index Database Server phai mat kha nhieu thi gian. Co mot vai trng hp chnh cac index lam cho chung cham han. Neu nh tren table cua ban tat ca cac record eu giong y nh nhau th khong co ly do g e ban tao index. Cac index d tha ch lam cho ton them khong gian a cua ban ma thoi.

oi vi mot table co gan khoa chnh (primary key) th thong thng field co khoa nay c dung vao viec tm kiem cho nen index se c gan t ong tren field nay. Ban se gap lai rat nhieu cong viec tao index cac phan tiep theo sau.

Lenh CREATE Databasemysql> create database database_name;

Trc khi tao c mot Table th ieu tat yeu la ban phai tao c mot Database cai a. Viec nay de dang va nhanh chong thoi. Lenh CREATE c s dung nh sau:

Neu nh ban thac mac rang sau khi tao database no se nam trong th muc nao tren o a cua ban th ban hay tm trong ..\mysql\data xem co khong.

Khi at ten cho database, hay at ten cho field va index g ay tranh trng tranh trng hp at nhng cai ten kho nh hoac de b lan lon. oi vi mot so he thong Unix chang han co s phan biet ch HOA/thng th CSDL chay tren no cung anh hng theo. Ban hay chon mot quy c cho rieng mnh trong viec at ten e khoi nham lan ve sau. Chang han ten cua table va field eu at ch thng chang han. Nen nh la khong c s dung khoang trang.

Bay gi ban tm hieu ca hai cach tao database: Cach th nhat tao thu cong t dau nhac dong lenh DOS, cach th hai s dung cac lenh trong PHP. Cach th nhat toi a co trnh bay chng gii thieu va ban a tao mot database ten la guestbook. Cu phap tao nh sau:mysql> create database guestbook;

Cach th hai la s dung lenh trong PHP, ban co the dung ham mysql_create_db() hoac mysql_query(). Nhng nen nh trc khi tao ban phai thc hien c ket noi vi database server.$conn = mysql_connect(localhost,username, password) or die (Could not connect to localhost); mysql_create_db(my_database) or die (Could not create database); $string = create database my_other_db; mysql_query($string) or die(mysql_error());

Lenh USE Database

Sau khi a tao c mot database mi trong database server ban se bat au chon no e s dung cho cong viec cua mnh. Cach thc hien nh sau: 1. Command Prompt:mysql> use database_name;

2. Trong PHP:$conn = mysql_connect(localhost,username, password) or die (Could not connect to localhost); mysql_select_db(test, $conn) or die (Could not select database);

Lenh CREATE Table

Lu y: Lenh nay thc hien sau khi a co lenh CREATE Database. Mot khi ban a tao va chon database, viec tiep theo la tao mot table. Ban se s dung lenh Create Table nh sau:create table table_name( column_1 column_type column_attributes, column_2 column_type column_attributes, primary key (column_name), index index_name(column_name) )

oi vi thuoc tnh cac field (cot) chung ta can ban ve: - null hoac not null - default

Neu ban khong nh ngha NULL hay NOT NULL th NULL se c chon lam gia tr mac nh. Hay xet v du sau:create table topics2 ( topic_id integer not null auto_increment, parent_id integer default 0 not null, root_id integer default 0, name varchar(255), description text null, create_dt timestamp, modify_dt timestamp, author varchar(255) null, author_host varchar(255) null, primary key(topic_id), index my_index(parent_id))

Trong v du tren ban tao ra mot table co ten topics2, co tat ca 8 field va co 2 index, mot index cho khoa chnh va mot cho parent_id. Type cua cac field tren lan lt la integer, varchar, text, timestamp. Gia tr ng sau defaul la gia tr mac nh ban gan cho mot o trong record khi khong nhap lieu vao.

Bay gi chung ta ap dung cac lenh nay vao mot chng trnh PHP e tao table, ham mysql_query () c s dung:$conn = mysql_connect(localhost,username, password) or die (Could not connect to localhost); mysql_select_db(test, $conn) or die(could not select database); $query = create table my_table (col_1 int not null primary key, col_2 text); mysql_query($query) or die(mysql_error());

* Lu y: username va password tuy thuoc vao MySQL cua ban.V du:$conn = mysql_connect(localhost,minhtrung, zadfdfaked) orThong thng cac nha cung cap host PHP s dung localhost, tuy nhien mot so hosting khong s dung localhost nh Yahoo chang han. Do o ban can xem hng dan cua nha cung cap host.

Kieu d lieu

Bay gi chung ta hay ban ve cac kieu d lieu (type) gan cho field trong table. Co rat nhieu kieu khac nhau chung ta se lan lt khao sat tng kieu mot.

Kieu chuoi van banMySQL co 7 kieu danh cho d lieu kieu chuoi van ban: char varchar tinytext text mediumtext longtext enum

CHAR Cach s dung: char(length) Length co gia tr toi a la 255. Gia s ban s dung khai bao char(10) th ban ch c phep nhap vao toi a 10 ky t ma thoi. VARCHAR Cach s dung: varchar(length) Kieu nay cung gan giong nh kieu CHAR co o dai toi a cung la 255. iem khac biet cua varchar la no ch la bien lu tr o dai, cho nen no se khong thay oi khi gia tr cua cua o d lieu dai hay ngan. MySQL se sinh ra mot ky dung lam bien cha o dai cua field kieu varchar. ong thi MySQL se thc hien chc nang loai bo cac khoang trong trong moi o d lieu neu nh khong c s dung het. USING CHAR OR VARCHAR Co s khac nhau trong viec s dung CHAR va VARCHAR. Sau ay la phng hng la chon cua ban. Gia du ban tao mot field la A CH va ban d tnh o dai toi a la 150. Co nhng trng hp a ch rat ngan v du: 1 Le Li, Q.1, TPHCM. Ban ch s dung co 20 ky