PHP 初階 - 課程五 Benson Lu 2015
DatabaseTABLE
How Does My Data Get Stored? Ans: tables
Databases use a series of Tables to store the data.
first name
last name email
Benson Lu [email protected]
Cherry Lin [email protected]
#_asukademy_users
Databaseid
How Do I Organize My Data? Ans: id
For each record, we give a
id first name
last name email
1 Benson [email protected]
m
2 Cherry Lin [email protected]
#__asukademy_users
unique id (Primary key)
Database
One database may have multiple tables
id first name
last name email
1 Benson Lu [email protected]
2 Cherry Lin [email protected]
#__asukademy_users
id city area
3 Taichung South
4 Taipei Xinyi
#__asukademy_address
Database
Table may have relation with other tables
id first name
last name email addres
s_id
1 Benson Lu
llwbenson@gmail.
com4
2 Cherry Lin [email protected] 3
#__asukademy_users
id city area
3 Taichung South
4 Taipei Xinyi
#__asukademy_addresses
Create your DatabasePlan your table
1. decide what information you want to store 2. decide what TYPE is that data ex: date, varchar, char, 3. create table 4. create table structure
Table data TYPEType
型態 空間需求 範圍
TINYINT 1 Signed: -128 to 127 Unsigned: 0 to 255
INT 4 Signed: -2147683648~2147683647 Unsigned: 0~4294967295
DATE 3 1000-01-01~9999-12-31
File Structuremysqli.php
Database 物件, 拿來與 mysqli 做連線,下指令create.php
update.phpdelete.php
接收使⽤用者指令,並使⽤用 db 物件操作資料庫list.php
item.php
顯⽰示畫⾯面
update.php
Read from DatabaseSELECT * FROM user
1. Make connection2. SELECT * from table_name3. execute query4. parse result($result->num_rows)5. Close connection
Create from DatabaseINSERT INTO table (column1, column2, …)
VALUE(NULL, val2, val3, …)
1. Make connection2. INSERT INTO FOO3. execute query4. Close connection
Update from Database
1. We must have ID to determine which record to modified
2. We must know the what columns to update3. Make connection4. UPDATE table SET foo=bar …….. WHERE id=?5. execute query6. Close Connection
UPDATE table_name SET `column1`=`val1` … WHERE `id`=3
Delete from Database
1. We must have ID to determine which record to delete
2. Make connection 3. DELETE FROM users WHERE id = ?4. execute query5. Close Connection