際際滷

際際滷Share a Scribd company logo
L?p tr━nh v┐ Thi?t k? Web 1
? 2007 Khoa C?ng ngh? th?ng tin
KhoaKhoa CNTTCNTT CC ?H?H.KHTN.KHTN
B┐i 9
ThaoThao tt││cc CSDLCSDL vv??ii PHPPHP
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
C│c b??c thao t│c CSDL trong PHP
1. K?t n?i v?i CSDL
2. X?y d?ng c?u truy v?n d? li?u
3. Th?c thi c?u truy v?n
4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT
5. Ng?t k?t n?i v?i CSDL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
C│c b??c thao t│c CSDL trong PHP
1. K?t n?i v?i CSDL
2. X?y d?ng c?u truy v?n d? li?u
3. Th?c thi c?u truy v?n
4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT
5. Ng?t k?t n?i v?i CSDL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
1. T?o K?t n?i CSDL
K?t n?i MySQL v┐ Ch?n CSDL
C resource mysql_connect([string host], [string username],
[string password])
C int mysql_select_db (string database, [resource connection])
<?php
$connection = mysql_connect(^localhost",^root ̄,^root");
mysql_select_db(^TestDB", $connection);
?>
DatabaseApplication
Connection
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
C│c b??c thao t│c CSDL trong PHP
1. K?t n?i v?i CSDL
2. X?y d?ng c?u truy v?n d? li?u
3. Th?c thi c?u truy v?n
4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT
5. Ng?t k?t n?i v?i CSDL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
www.example.com
Webserver
2. X?y d?ng c?u truy v?n
Y┷u c?u trang xlDangnhap.php
Textbox : txtDangnhap
Password Box: txtMatkhau
txtMatkhau = 123
txtDangnhap = admin
Database
Server
strSQL = ^SELECT *
FROM Users
WHERE UserName =
`admin¨ and Password =
`123¨ ^
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
2. X?y d?ng c?u truy v?n (tt)
SQL V┴ d?
INSERT strSQL = "INSERT INTO Users (UserName, Password) VALUES (`admin',`123')^
DELETE strSQL = "DELETE FROM Users WHERE UserName = `minh'^
UPDATE strSQL = "UPDATE Users SET Password = `abc' WHERE UserName = `admin' ^
SELECT strSQL = ^SELECT * FROM Users WHERE UserName = `admin' ^
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
C│c b??c thao t│c CSDL trong PHP
1. K?t n?i v?i CSDL
2. X?y d?ng c?u truy v?n d? li?u
3. Th?c thi c?u truy v?n
4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT
5. Ng?t k?t n?i v?i CSDL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
3. Th?c thi c?u truy v?n
resource mysql_query(string SQL_command, [resource connection])
<?php
$strSQL = "INSERT INTO Users (UserName, Password) VALUES (`admin',`123')^;
mysql_query($strSQL);
$strSQL = ^SELECT * FROM Users^;
$result = mysql_query($strSQL);
?>
Application
Connection 011010011011010011
query
Database
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
C│c b??c thao t│c CSDL trong PHP
1. K?t n?i v?i CSDL
2. X?y d?ng c?u truy v?n d? li?u
3. Th?c thi c?u truy v?n
4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT
5. Ng?t k?t n?i v?i CSDL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
4. X? l? k?t qu? tr? v?
Hi?n th? d? li?u tr? v?
C S? d?ng h┐m
$row = mysql_fetch_array($result)
C S? l??ng FIELD
$num = mysql_num_fields($result);
C Truy c?p ??n t?ng FIELD
echo $row[^UserName ̄];
Ho?c
echo $row[0];
UserName Password
A AA
B BB
C CC
D DD
´ ´
X XX
Y YY
Z ZZ
cursor
V┴ d?: Xu?t records t? CSDL ra trang HTML
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
C│c b??c thao t│c CSDL trong PHP
1. K?t n?i v?i CSDL
2. X?y d?ng c?u truy v?n d? li?u
3. Th?c thi c?u truy v?n
4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT
5. Ng?t k?t n?i v?i CSDL
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
5. Ng?t k?t n?i d? li?u
int mysql_close([resource connection])
T? ??ng th?c thi khi k?t th┣c m? l?nh
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
T?ng h?p c│c b??c thao t│c CSDL MySQL
<?php
// 1. Ket noi CSDL
$connection = mysql_connect(^hostname",^username",^password");
mysql_select_db(^database_name", $connection);
// 2. Chuan bi cau truy van & 3. Thuc thi cau truy van
$strSQL = "SELECT * FROM TableName ̄;
$result = mysql_query($strSQL);
// 4.Xu ly du lieu tra ve
while ($row = mysql_fetch_array($result))
{
for ($i=0; $i<mysql_num_fields($result); $i++)
echo $row[$i] . " ";
}
// 5. Dong ket noi
mysql_close($connection);
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? h┐m thao t│c v?i CSDL MySQL
int mysql_data_seek(resource result_set, int row)
array mysql_fetch_row(resource result_set)
object mysql_fetch_object(resource result_set, [int result_type])
int mysql_free_result(resource result_set)
int mysql_num_rows(resource result_set)
resource mysql_pconnect([string host:port], [string user],
[string password])
resource mysql_unbuffered_query(string query,
[resource connection])
object mysql_fetch_field(resource result_set, [int attribute_number])
int mysql_affected_rows([resource connection])
int mysql_insert_id([resource connection])
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? v?n ?? - MySQL & Font Unicode
Font UNICODE
<?php
mysql_query(^set names `utf-8¨ ̄);
?>
Encode
Decode
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? v?n ?? - X? l? l?i
int mysql_errno(resource connection)
string mysql_error(resource connection)
<?php
function showerror( ) {
die("Error " . mysql_errno( ) . " : " . mysql_error( ));
}
if (!($connection = @ mysql_connect("localhost", "fred","shhh")))
die("Could not connect");
If (!(mysql_select_db("winestor", $connection)))
showerror( );
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? v?n ?? - Include File
<?php
$hostName = "localhost";
$databaseName = "bookstoreDB";
$username = "root";
$password = "root";
?>
<?php
function showError( )
{
die("Error " . mysql_errno( ) . " : " . mysql_error( ));
}
?>
<?php
include `db.inc¨;
include `error.inc¨;
?>
db.inc
error.inc
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? v┴ d?:
Tr━nh b┐y d? li?u ra trang Web C T━m ki?m & S?p x?p
T━m ki?m
Xu?t d? li?u tho? ?i?u ki?n
S?p x?p records theo t┷n tr??ng
Cho ph└p ng??i d┫ng chon c?t ?? s?p x?p
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? v┴ d?:
T?ng h?p Th┷m, X┏a v┐ S?a d? li?u
Add
Delete
Edit
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
M?t s? v┴ d?:
Ph?n trang d? li?u
Tr??ng h?p d? li?u l?y v? c┏ qu│ nhi?u m?u tin (hi?n th? b?ng
qu│ d┐i)
Chia d? li?u hi?n th? tr┷n nhi?u trang
C S? m?u tin hi?n th? tr┷n 1 trang: $rowsPerPage
C S? th? t? trang hi?n th?: $pageNum=1,2´
C Ch? s? m?u tin b?t ??u trong trang th? $pageNum:
$offset = ($pageNum - 1) * $rowsPerPage;
C T?ng s? m?u tin tr? v?: $numRows
C T?ng s? trang hi?n th?:
$maxPage = ceil($numRows/$rowsPerPage);
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
Ph?n trang d? li?u (tt)
C│c b??c th?c hi?n ph?n trang
CT┴nh c│c th?ng s? ph?n trang
CL?y th?ng tin c?n hi?n th?
CHi?n th? th?ng tin c?a trang hi?n t?i
CT?o li┷n k?t ch? ??n c│c trang
C Xem V┴ d?
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
N?i dung
C│c b??c thao t│c v?i CSDL
M?t s? v?n ?? khi thao t│c v?i CSDL
M?t s? v┴ d? minh h?a:
C T━m ki?m v┐ s?p x?p
C T?ng h?p Th┷m, X┏a, S?a
C Ph?n trang d? li?u
Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
Thao t│c v?i Microsoft SQL Server
<?php
// 1. Ket noi CSDL
$connection = mssql_connect(^hostname",^username",^password");
mysql_select_db(^database_name", $connection);
// 2. Chuan bi cau truy van & 3. Thuc thi cau truy van
$strSQL = "SELECT * FROM TableName ̄;
$result = mssql_query($strSQL);
// 4.Xu ly du lieu tra ve
while ($row = mssql_fetch_array($result))
{
for ($i=0; $i<mssql_num_fields($result); $i++)
echo $row[$i] . " ";
}
// 5. Dong ket noi
mssql_close($connection);
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
Thao t│c v?i Microsoft Access C S? d?ng ODBC
<?php
// 1. Ket noi CSDL
$connection = odbc_connect("mobileDB","","");
// 2. Chuan bi cau truy van & 3. Thuc thi cau truy van
$strSQL = "SELECT * FROM TableName ̄;
$result = odbc_exec ($connection, $strSQL);
// 4.Xu ly du lieu tra ve
while (odbc_fetch_row($result))
{
for ($i=1; $i<=odbc_num_fields($result); $i++)
echo odbc_result($result, $i) . " ";
}
// 5. Dong ket noi
odbc_close($connection);
?>
L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP
? 2007 Khoa CNTT - ?HKHTN
Thao t│c v?i Microsoft Access C S? d?ng ADO
<?php
// 1. Ket noi CSDL
$db = "db/bookStoreDB.mdb";
$conn = new COM("ADODB.Connection") ;
$sql = "DRIVER={Microsoft Access Driver (*.mdb)} ;
DBQ=". realpath($db) ." ; uid=; pwd=;" ;
$conn->open($sql);
// 2. Chuan bi cau truy van & 3. Thuc thi cau truy van
$strSQL = "SELECT * FROM TableName ̄;
$rs = $conn->execute($strSQL);
// 4.Xu ly du lieu tra ve
while (!$rs->EOF) {
echo $rs->Fields[`FieldName']->Value . " ";
$rs->MoveNext() ;
}
// 5. Dong ket noi
$rs->Close() ;
$conn->Close() ;
?>

More Related Content

Web course php & csdl

  • 1. L?p tr━nh v┐ Thi?t k? Web 1 ? 2007 Khoa C?ng ngh? th?ng tin KhoaKhoa CNTTCNTT CC ?H?H.KHTN.KHTN B┐i 9 ThaoThao tt││cc CSDLCSDL vv??ii PHPPHP
  • 2. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 3. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 4. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN C│c b??c thao t│c CSDL trong PHP 1. K?t n?i v?i CSDL 2. X?y d?ng c?u truy v?n d? li?u 3. Th?c thi c?u truy v?n 4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT 5. Ng?t k?t n?i v?i CSDL
  • 5. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN C│c b??c thao t│c CSDL trong PHP 1. K?t n?i v?i CSDL 2. X?y d?ng c?u truy v?n d? li?u 3. Th?c thi c?u truy v?n 4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT 5. Ng?t k?t n?i v?i CSDL
  • 6. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN 1. T?o K?t n?i CSDL K?t n?i MySQL v┐ Ch?n CSDL C resource mysql_connect([string host], [string username], [string password]) C int mysql_select_db (string database, [resource connection]) <?php $connection = mysql_connect(^localhost",^root ̄,^root"); mysql_select_db(^TestDB", $connection); ?> DatabaseApplication Connection
  • 7. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN C│c b??c thao t│c CSDL trong PHP 1. K?t n?i v?i CSDL 2. X?y d?ng c?u truy v?n d? li?u 3. Th?c thi c?u truy v?n 4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT 5. Ng?t k?t n?i v?i CSDL
  • 8. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN www.example.com Webserver 2. X?y d?ng c?u truy v?n Y┷u c?u trang xlDangnhap.php Textbox : txtDangnhap Password Box: txtMatkhau txtMatkhau = 123 txtDangnhap = admin Database Server strSQL = ^SELECT * FROM Users WHERE UserName = `admin¨ and Password = `123¨ ^
  • 9. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN 2. X?y d?ng c?u truy v?n (tt) SQL V┴ d? INSERT strSQL = "INSERT INTO Users (UserName, Password) VALUES (`admin',`123')^ DELETE strSQL = "DELETE FROM Users WHERE UserName = `minh'^ UPDATE strSQL = "UPDATE Users SET Password = `abc' WHERE UserName = `admin' ^ SELECT strSQL = ^SELECT * FROM Users WHERE UserName = `admin' ^
  • 10. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN C│c b??c thao t│c CSDL trong PHP 1. K?t n?i v?i CSDL 2. X?y d?ng c?u truy v?n d? li?u 3. Th?c thi c?u truy v?n 4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT 5. Ng?t k?t n?i v?i CSDL
  • 11. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN 3. Th?c thi c?u truy v?n resource mysql_query(string SQL_command, [resource connection]) <?php $strSQL = "INSERT INTO Users (UserName, Password) VALUES (`admin',`123')^; mysql_query($strSQL); $strSQL = ^SELECT * FROM Users^; $result = mysql_query($strSQL); ?> Application Connection 011010011011010011 query Database
  • 12. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN C│c b??c thao t│c CSDL trong PHP 1. K?t n?i v?i CSDL 2. X?y d?ng c?u truy v?n d? li?u 3. Th?c thi c?u truy v?n 4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT 5. Ng?t k?t n?i v?i CSDL
  • 13. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN 4. X? l? k?t qu? tr? v? Hi?n th? d? li?u tr? v? C S? d?ng h┐m $row = mysql_fetch_array($result) C S? l??ng FIELD $num = mysql_num_fields($result); C Truy c?p ??n t?ng FIELD echo $row[^UserName ̄]; Ho?c echo $row[0]; UserName Password A AA B BB C CC D DD ´ ´ X XX Y YY Z ZZ cursor V┴ d?: Xu?t records t? CSDL ra trang HTML
  • 14. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN C│c b??c thao t│c CSDL trong PHP 1. K?t n?i v?i CSDL 2. X?y d?ng c?u truy v?n d? li?u 3. Th?c thi c?u truy v?n 4. X? l┴ k?t qu? tr? v? t? c?u truy v?n SELECT 5. Ng?t k?t n?i v?i CSDL
  • 15. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN 5. Ng?t k?t n?i d? li?u int mysql_close([resource connection]) T? ??ng th?c thi khi k?t th┣c m? l?nh
  • 16. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN T?ng h?p c│c b??c thao t│c CSDL MySQL <?php // 1. Ket noi CSDL $connection = mysql_connect(^hostname",^username",^password"); mysql_select_db(^database_name", $connection); // 2. Chuan bi cau truy van & 3. Thuc thi cau truy van $strSQL = "SELECT * FROM TableName ̄; $result = mysql_query($strSQL); // 4.Xu ly du lieu tra ve while ($row = mysql_fetch_array($result)) { for ($i=0; $i<mysql_num_fields($result); $i++) echo $row[$i] . " "; } // 5. Dong ket noi mysql_close($connection); ?>
  • 17. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? h┐m thao t│c v?i CSDL MySQL int mysql_data_seek(resource result_set, int row) array mysql_fetch_row(resource result_set) object mysql_fetch_object(resource result_set, [int result_type]) int mysql_free_result(resource result_set) int mysql_num_rows(resource result_set) resource mysql_pconnect([string host:port], [string user], [string password]) resource mysql_unbuffered_query(string query, [resource connection]) object mysql_fetch_field(resource result_set, [int attribute_number]) int mysql_affected_rows([resource connection]) int mysql_insert_id([resource connection])
  • 18. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 19. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? v?n ?? - MySQL & Font Unicode Font UNICODE <?php mysql_query(^set names `utf-8¨ ̄); ?> Encode Decode
  • 20. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? v?n ?? - X? l? l?i int mysql_errno(resource connection) string mysql_error(resource connection) <?php function showerror( ) { die("Error " . mysql_errno( ) . " : " . mysql_error( )); } if (!($connection = @ mysql_connect("localhost", "fred","shhh"))) die("Could not connect"); If (!(mysql_select_db("winestor", $connection))) showerror( ); ?>
  • 21. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? v?n ?? - Include File <?php $hostName = "localhost"; $databaseName = "bookstoreDB"; $username = "root"; $password = "root"; ?> <?php function showError( ) { die("Error " . mysql_errno( ) . " : " . mysql_error( )); } ?> <?php include `db.inc¨; include `error.inc¨; ?> db.inc error.inc
  • 22. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 23. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 24. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? v┴ d?: Tr━nh b┐y d? li?u ra trang Web C T━m ki?m & S?p x?p T━m ki?m Xu?t d? li?u tho? ?i?u ki?n S?p x?p records theo t┷n tr??ng Cho ph└p ng??i d┫ng chon c?t ?? s?p x?p
  • 25. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 26. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? v┴ d?: T?ng h?p Th┷m, X┏a v┐ S?a d? li?u Add Delete Edit
  • 27. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 28. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN M?t s? v┴ d?: Ph?n trang d? li?u Tr??ng h?p d? li?u l?y v? c┏ qu│ nhi?u m?u tin (hi?n th? b?ng qu│ d┐i) Chia d? li?u hi?n th? tr┷n nhi?u trang C S? m?u tin hi?n th? tr┷n 1 trang: $rowsPerPage C S? th? t? trang hi?n th?: $pageNum=1,2´ C Ch? s? m?u tin b?t ??u trong trang th? $pageNum: $offset = ($pageNum - 1) * $rowsPerPage; C T?ng s? m?u tin tr? v?: $numRows C T?ng s? trang hi?n th?: $maxPage = ceil($numRows/$rowsPerPage);
  • 29. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN Ph?n trang d? li?u (tt) C│c b??c th?c hi?n ph?n trang CT┴nh c│c th?ng s? ph?n trang CL?y th?ng tin c?n hi?n th? CHi?n th? th?ng tin c?a trang hi?n t?i CT?o li┷n k?t ch? ??n c│c trang C Xem V┴ d?
  • 30. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN N?i dung C│c b??c thao t│c v?i CSDL M?t s? v?n ?? khi thao t│c v?i CSDL M?t s? v┴ d? minh h?a: C T━m ki?m v┐ s?p x?p C T?ng h?p Th┷m, X┏a, S?a C Ph?n trang d? li?u Thao t│c v?i c│c H? Qu?n tr? CSDL kh│c
  • 31. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN Thao t│c v?i Microsoft SQL Server <?php // 1. Ket noi CSDL $connection = mssql_connect(^hostname",^username",^password"); mysql_select_db(^database_name", $connection); // 2. Chuan bi cau truy van & 3. Thuc thi cau truy van $strSQL = "SELECT * FROM TableName ̄; $result = mssql_query($strSQL); // 4.Xu ly du lieu tra ve while ($row = mssql_fetch_array($result)) { for ($i=0; $i<mssql_num_fields($result); $i++) echo $row[$i] . " "; } // 5. Dong ket noi mssql_close($connection); ?>
  • 32. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN Thao t│c v?i Microsoft Access C S? d?ng ODBC <?php // 1. Ket noi CSDL $connection = odbc_connect("mobileDB","",""); // 2. Chuan bi cau truy van & 3. Thuc thi cau truy van $strSQL = "SELECT * FROM TableName ̄; $result = odbc_exec ($connection, $strSQL); // 4.Xu ly du lieu tra ve while (odbc_fetch_row($result)) { for ($i=1; $i<=odbc_num_fields($result); $i++) echo odbc_result($result, $i) . " "; } // 5. Dong ket noi odbc_close($connection); ?>
  • 33. L?p tr━nh v┐ Thi?t k? Web 1 C B┐i 9: Thao t│c CSDL v?i PHP ? 2007 Khoa CNTT - ?HKHTN Thao t│c v?i Microsoft Access C S? d?ng ADO <?php // 1. Ket noi CSDL $db = "db/bookStoreDB.mdb"; $conn = new COM("ADODB.Connection") ; $sql = "DRIVER={Microsoft Access Driver (*.mdb)} ; DBQ=". realpath($db) ." ; uid=; pwd=;" ; $conn->open($sql); // 2. Chuan bi cau truy van & 3. Thuc thi cau truy van $strSQL = "SELECT * FROM TableName ̄; $rs = $conn->execute($strSQL); // 4.Xu ly du lieu tra ve while (!$rs->EOF) { echo $rs->Fields[`FieldName']->Value . " "; $rs->MoveNext() ; } // 5. Dong ket noi $rs->Close() ; $conn->Close() ; ?>