Top Banner
PHP 初階 - 課程五 Benson Lu 2015
22

PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Jul 19, 2015

Download

Software

Li-Wei Lu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

PHP 初階 - 課程五 Benson Lu

2015

Page 2: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

OVERVIEWUsing database with MySQLi

CRUD

Page 3: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

CRUD

Create - Read - Update - Delete新增 讀取 修改 刪除

Page 4: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

What is Database資料庫

A database is an organized collection of data.

Page 5: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 6: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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)

Page 7: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 8: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 9: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Create your DatabaseCREATE

Page 10: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 11: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Table data TYPEType

Page 12: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 13: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

CRUD

Create - Read - Update - Delete新增 讀取 修改 刪除

Page 14: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

File Structuremysqli.php

Database 物件, 拿來與 mysqli 做連線,下指令create.php

update.phpdelete.php

接收使⽤用者指令,並使⽤用 db 物件操作資料庫list.php

item.php

顯⽰示畫⾯面

update.php

Page 15: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Connect to Databasemysqli($host, $username, $password, $db_name)

Page 16: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Read from DatabaseSELECT * FROM user

1. Make connection2. SELECT * from table_name3. execute query4. parse result($result->num_rows)5. Close connection

Page 17: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Read from DatabaseSELECT * FROM user

Page 18: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Create from DatabaseINSERT INTO table (column1, column2, …)

VALUE(NULL, val2, val3, …)

1. Make connection2. INSERT INTO FOO3. execute query4. Close connection

Page 19: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 20: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Update to Database

Page 21: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

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

Page 22: PHP 初階課程 Part. 5 - MySQLi with PHP, Basic CRUD

Thank You