Professional Documents
Culture Documents
H Ch Minh
Ni dung
1. 2. 3. 4. 5. Cc ki u d li u c b n trong MySQL Cc l nh thng d ng trong MySQL K t n i MySQL t PHP Quy trnh k t n i vo MySQL Cc b c truy c p CSDL MySQL
1. Cc ki u d
Ki u
char(length) char(length) varchar(length) varchar(length) text int(length) int(length) decimal(length,dec) decimal(length,dec)
li u c b n trong MySQL
M t
t i a 255 k t , chi u di c nh = length t i a 255 k t , chi u di ng <= length t i a 65536 k t -2.147.483.648 n +2.147.483.647 t i a length ch s trong dec ch s th p phn
M t
t p h p t nh, ngha t i a 65.535 gi tr yyyy-mmyyyy-mm-dd hh:mm:ss yyyy-mmyyyy-mm-dd hh:mm:ss
M t
t o CSDL ho c b ng thay i b ng c s n ch n d li u t b ng xa d li u kh i b ng xem thng tin m t v c u trc b ng ghi gi tr vo b ng c p nh t d li u c trong b ng xa b ng hay ton b CSDL
[CONSTRAINTS], [CONSTRAINTS],
CREATE USER username[@hostname] [IDENTIFIED BY username[@hostname] [PASSWORD] 'password']; 'password']; CREATE [OR REPLACE] VIEW viewname AS SELECT ...;
6
ADD column datatype [NULL|NOT NULL] [CONSTRAINTS], CHANGE column columns datatype [NULL|NOT NULL] [CONSTRAINTS], DROP column,
... );
2. Cc l nh thng d ng trong MySQL DELETE FROM tablename [WHERE ...]; VD: DELETE FROM ketqua WHERE MaSV = K29.103.010
11
12
13
14
3. K t n i MySQL t
PHP script
Th vi n chu n
PHP
Th vi n c i ti n
mysql
S d ng hm S d ng l p
mysqli
S d ng hm
mysql
mysqli
15
u i m
H tr l p trnh h ng i t ng H tr nhn b n v phn tn CSDL Nn v m ha d li u trn k t n i T i u hi u nng v m
16
17
B c 1: M k t n i n CSDL
// OOP mysqli mysqli('hostname', 'username', $mysqli = new mysqli('hostname', 'username', 'password', 'dbname'); password', 'dbname'); // mysqli $link = mysqli_connect('hostname', mysqli_connect('hostname', 'username', 'password', 'dbname'); username', 'password', 'dbname');
18
B c 2: Ch n CSDL
// OOP mysqli $mysqli->select_db('dbname'); $mysqli->select_db('dbname'); // mysqli mysqli_select_db($link, mysqli_select_db($link, 'dbname');
19
B c 3: Ch n b ng m (n u c n)
// OOP mysqli mysqli->query($link, mysqli->query($link, "SET NAMES character set") character set") // mysqli mysqli_query($link, mysqli_query($link, "SET NAMES character set") character set")
20
B c 4: X
Truy v n
l CSDL
// OOP mysqli $result = mysqli->query(query") mysqli->query(query") // mysqli $result = mysqli_query($link, query") mysqli_query($link,
21
B c 4: X
L y d li u t
l CSDL (tt)
truy v n
// OOP mysqli $row = $result->fetch_row() $result$row = $result->fetch_assoc() $result$row = $result->fetch_array(result_type) $result->fetch_array(result_type) // mysqli $row = mysqli_fetch_row($result) mysqli_fetch_row($result) $row = mysqli_fetch_assoc($result) mysqli_fetch_assoc($result) $row = mysqli_fetch_array($result, result_type) mysqli_fetch_array($result, result_type)
22
B c 5: D n d p
// OOP mysqli $result->close() $result->close() // mysqli mysqli_free_result($result) mysqli_free_result($result)
23
B c 6: ng k t n i
// OOP mysqli
$mysqli$mysqli->close()
// mysqli
mysqli_close($link)
24
25
Ch n Database lm vi c
$dbname =bookstore; mysqli_select_db($dbname);
Gi i phng Database
mysqli_close($conn);
26
S d ng k t qu truy v n:
mysqli_fetch_array($result); mysqli_fetch_row($result); mysqli_fetch_assoc($result);
S d ng ti ng vi t:
mysqli_query(SET CHARACTER SET UTF8); Hay mysqli_query(SET NAMES UTF8);
27
Lu
Hm die(Chu i): a ra thng bo v k t thc. V i cch vi t trn, die ch th c hi n khi l nh tr c n khng thnh cng Cc hm c n thi t:
mysqli_affected_rows(): S b n ghi b tc ng b i l nh mysqli_query li n tr c. mysqli_error(): Thng bo l i (n u c) mysqli_errno(): M l i
28
Xem thm
K t n i CSDL MySQL s d ng PDO
29
Windows
extension=php_pdo.dll extension=php_pdo_XXX.dll
30
31
32
Connection management
try { $dbh = new PDO($dsn, $user, $pw); } catch (PDOException $e) { echo connect failed: . $e->getMessage(); } // use the database here // // done; release the connection $dbh = null;
33
34
Fetch types
$stmt->fetch(PDO_FETCH_BOTH)
Array with numeric and string keys default option
PDO_FETCH_NUM
Array with numeric keys
PDO_FETCH_ASSOC
Array with string keys
PDO_FETCH_OBJ
$obj->name holds the name column from the row
PDO_FETCH_BOUND
Just returns true until there are no more rows
35
36
Smarter Queries
Quoting is annoying, but essential PDO offers a better way $stmt->prepare(INSERT INTO CREDITS (extension, name) VALUES (:extension, :name)); $stmt->execute(array( :extension => xdebug, :name => Derick Rethans ));
37
38
Transactions
try { $dbh->beginTransaction(); $dbh->query(UPDATE ); $dbh->query(UPDATE ); $dbh->commit(); } catch (PDOException $e) { $dbh->rollBack(); }
39
Cu hi v tho lun
40