Download - PHP-5 ติดต่อฐานข้อมูล MySQL

Transcript
Page 1: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP-5ตดตอฐานขอมล

MYSQL 1

Page 2: PHP-5 ติดต่อฐานข้อมูล  MySQL

MySQL Introduction ทำ�คว�มรจกกบฐ�นขอมล MySQL (ม�ยเอสควแอล) เปนระบบจดก�รฐ�นขอมลโดยใชภ�ษ� SQL. แมว� MySQL เปนซอฟตแวรโอเพนซอรส แตแตกต�งจ�กซอฟตแวรโอเพนซอรสทวไป โดยมก�รพฒน�ภ�ยใตบรษท MySQL AB ในประเทศสวเดน โดยจดก�ร MySQL ทงในแบบทใหใชฟร และแบบทใชในเชงธรกจ

MySQL สร�งขนโดยช�วสวเดน 2 คน และช�วฟนแลนด ชอ David Axmark, Allan Larsson และ Michael "Monty" Widenius.

ปจจบนบรษทซนไมโครซสเตมส (Sun Microsystems, Inc.) เข�ซอกจก�รของ MySQL AB เรยบรอยแลว ฉะนนผลตภณฑภ�ยใต MySQL AB ทงหมดจะตกเปนของซน

2

Page 3: PHP-5 ติดต่อฐานข้อมูล  MySQL

รนของผลตภณฑ

รนของผลตภณฑนนแบงออกม�ไดส�มส�ยก�รผลต ไดแก เวอรชนใชฟร เวอรชนก�รค� และเวอรชนทสนบสนนกบผลตภณฑ SAP (MAX DB) คว�มแตกต�งคอเวอรชนคอมมวนตนนส�ม�รถนำ�ไปใชง�นไดฟรแตข�ดก�รสนบ สนนหรอก�รชวยเหลอเมอมปญห�เกดขน, เวอรชนทเปนคอมเมอรเชยลนนใหบรก�รด�นคว�มสนบสนนเมอมปญห� (ซอบรก�ร) สรปคร�วๆ ประเภทด�ต�เบสใหเลอกใชดงน

* MySQL เอนเทอรไพรส Enterprise* MySQL คลสเตอร Cluster* MySQL Embedded* MySQL Community (opensource เวอรชน) 3

Page 4: PHP-5 ติดต่อฐานข้อมูล  MySQL

ความสามารถทเพมขน

ในเวอรชน 5.0 มคว�มส�ม�รถหล�ยอย�งทสำ�คญสำ�หรบระบบฐ�นขอมลขน�ดใหญหรอระดบองคกร (EnterPrise Feature) เชน Store Procedure, database trigger, database view, database schema ซงไดมก�รปรบเพมประสทธภ�พในสวนของต�ร�งและก�รทำ�ดชน (index) ขนม�อก ปจจบนเวอรชน community หรอเวอรชนทเสถยร (stable) 5.0 และเวอรชนทดสอบคอ 5.1 beta release และ 5.2 Alpha

ตงแตเวอรชน 5.1 เรมสนบสนนก�รทำ� Parttion Database , ต�ร�งเวล�สำ�หรบเหตก�รณต�งๆ (Event schedule)

4

Page 5: PHP-5 ติดต่อฐานข้อมูล  MySQL

ประเภทการจดเกบขอมล (Database Storage Engine) ทสนบสนน

* MyISAM ค�ปกต (default)* InnoDB สนบสนนก�รทำ� ทร�นแซคชน (transaction) แบบ ACID* Memory ก�รจดเกบในหนวยคว�มจำ� ใชเปนต�ร�งชวคร�วเพอคว�มรวดเรว เนองจ�กเกบไวในหนวยคว�มจำ� ทำ�ใหมคว�มเรวในก�รทำ�ง�นสงม�ก* Merge* Archive เหม�ะสำ�หรบก�รจดเกบขอมลพวก log file,ขอมลทไมตองมก�ร ควร (query) หรอใชบอยๆ เชน log file เพอประโยชนในก�รตรวจสอบยอนหลง (Security Audit Information)* Federated สำ�หรบก�รจดเกบแบบปล�ยท�ง (remote server) แทนทจะเปนก�รจดเกบแบบ local เหมอนก�รจดเกบ (Storage) แบบอนๆ* NDB สำ�หรบก�รจดเกบแบบ คลสเตอร(cluster)* CSV เกบขอมลจ�ก Text ไฟลโดยอ�ศยเครองหม�ย คอมม� (comma) เปนตวแบงฟลด* Blackhole* Example

5

Page 6: PHP-5 ติดต่อฐานข้อมูล  MySQL

ชนดของขอมลทสนบสนน

ชนดขอมลท MySQL สนบสนนแบงเปนส�มประเภทหลกใหญๆ

ชนดขอมลทเปนตวเลขo BIT (มใชไดกบ MyISAM, InnoDB, Memory)o TINYINTo SMALLINTo MEDIUMINTo INTo BIGINT

6

Page 7: PHP-5 ติดต่อฐานข้อมูล  MySQL

* ชนดขอมลทเกยวกบวนทและเวลาo DATETIMEo DATEo TIMESTAMPo TIMEo YEAR

ชนดขอมลทเกยวกบตวอกษรo CHARo VARCHARo BINARYo VARBINARYo BLOBo TEXTo ENUMo SET

7

Page 8: PHP-5 ติดต่อฐานข้อมูล  MySQL

การใชงาน

MySQL เปนทนยมใชกนม�กสำ�หรบฐ�นขอมลสำ�หรบเวบไซต เชน มเดยวก และ phpBB และนยมใชง�นรวมกบภ�ษ�โปรแกรม PHP ซงมกจะไดชอว�เปนค จะเหนไดจ�กคมอคอมพวเตอรต�งๆ ทจะสอนก�รใชง�น MySQL และ PHP ควบคกนไป นอกจ�กน หล�ยภ�ษ�โปรแกรมทส�ม�รถทำ�ง�นรวมกบฐ�นขอมล MySQL ซงรวมถง ภ�ษ�ซ ซพลสพลส ป�สค�ล ซช�รป ภ�ษ�จ�ว� ภ�ษ�เพรล พเอชพ ไพทอน รบ และภ�ษ�อน ใชง�นผ�น API สำ�หรบโปรแกรมทตดตอผ�น ODBC หรอ สวนเชอมตอกบภ�ษ�อน (database connector) เชน เอเอสพ ส�ม�รถเรยกใช MySQL ผ�นท�ง MyODBC,ADO,ADO.NET เปนตน

โปรแกรมชวยในการจดการฐานขอมล และ ทำางานกบฐานขอมล

ในก�รจดก�รฐ�นขอมล MySQL คณส�ม�รถใชโปรแกรมแบบ command-line เพอจดก�รฐ�นขอมล (โดยใชคำ�สง: mysql และ mysqladmin เปนตน). หรอจะด�วนโหลดโปรแกรมจดก�รฐ�นขอมลแบบ GUI จ�กเวบไซตของ MySQL ซงคอโปรแกรม: MySQL Administrator และ MySQL Query Browser. เปนตน

8

Page 9: PHP-5 ติดต่อฐานข้อมูล  MySQL

สวนเชอมตอกบภาษาการพฒนาอน (database connector)มสวนตดตอ (interface) เพอเชอมตอกบภ�ษ�ในก�รพฒน� อนๆ เพอใหเข�ถงฟงกชนก�รทำ�ง�นกบฐ�นขอมล MySQL ไดเชน ODBC (Open Database Connector) อนเปนม�ตรฐ�นกล�งทกำ�หนดม�เพอใหใชเปนสะพ�นในก�รเชอมตอกบโปรแกรม หรอระบบอนๆ เชน MyODBC อนเปนไดรเวอรเพอใชสำ�หรบก�รเชอมตอในระบบปฏบตก�รวนโดว, JDBC คล�สสวนเชอมตอสำ�หรบ Java เพอใชในก�รตดตอกบ MySQL และม API (Application Programming Interface) ต�งๆมใหเลอกใชม�กม�ยในก�รทเข�ถง MySQL โดยไมขนอยกบภ�ษ�ก�รพฒน�ใดภ�ษ�หนง

นอกเหนอจ�ก ตวเชอมตอกบภ�ษ�อน (Connector) ทไดกล�วม�แลว ยงม API ทสนบสนนในขณะนคอ

* DBI สำ�หรบก�รเชอมตอกบ ภ�ษ� perl* Ruby สำ�หรบก�รเชอมตอกบ ภ�ษ� ruby* Python สำ�หรบก�รเชอมตอกบภ�ษ� python* .NET สำ�หรบก�รเชอมกบภ�ษ� .NET framework* MySQL++ สำ�หรบเชอมตอกบภ�ษ� C++* Ch สำ�หรบก�รเชอมตอกบ Ch (C/C++ interpreter)ยงมโปรแกรมอกตว เปนโปรแกรมบรห�รพฒน�โดยผอน ซงใชกนอย�งแพรหล�ยและนยมกนเขยนในภ�ษ�พเอชพ เปนโปรแกรมเวบแอปพลเคชน ชอ phpMyAdmin

9

Page 10: PHP-5 ติดต่อฐานข้อมูล  MySQL

MYSQL INSTALLATION ตดตง MYSQL หา MySQL ไดจากไหน??

MySQL เปน Software ทส�ม�รถใชง�นไดฟร โดยห�โหลดไดจ�กเวบไซต

http://www.mysql.com

หรอสำ�หรบผทเขยน PHP อยแลว ส�ม�รถตดตงตง Appserv ไดเลยครบ

Install Appserv (PHP+Apache+MySQL) http://www.thaicreate.com/php/php-install-apache.

html หรอห�กผทพฒน�โปรแกรมดวยภ�ษ� ASP หรอ ASP.NET แลวใช IIS เปน Web Server ถ�ห�กตดตง Appserv ผมแนะนำ�ใหเปลยน Port ของ Apache ใน Appserv เปน 81 หรออย�งอนทไมใช 80 เพอปองกนไมใหมผลกระทบกบ IIS และ Apache Web Server

หรอจะตดตงเฉพ�ะ MySQL โดยส�ม�รถห�ด�วนโหลดไดจ�กเวบไซต http://www.mysql.com

10

Page 11: PHP-5 ติดต่อฐานข้อมูล  MySQL

phpMyAdmin ทำ�คว�มรจก phpMyAdmin??? phpMyAdmin คออะไร ?phpMyAdmin เปนโปรแกรมทถกพฒน�ดวย PHP ทใชจดการ MySQL ผ�น Web Browser โดยส�ม�รถจดก�ร MySQL Database เกอบทกอย�ง เชนก�ร Create Database/Table/Field เพม/ลบ/แกไข ขอมลหรอโครงสร�งได

phpMyAdmin หาไดจากไหน ?ห�กผทตดตงโปรแกรม Appserv อยแลวส�ม�รถเรยกใช phpMyAdmin ไดทนท http://localhost/phpMyAdminphpMyAdmin มากบ Package ของ AppservAppserv & phpMyAdmin

http://www.thaicreate.com/php/php-install-apache.html หรอจะหาดาวนโหลด phpMyAdmin ไดจากเวบไซตDownload phpMyAdmin

http://www.phpmyadmin.net/ 11

Page 12: PHP-5 ติดต่อฐานข้อมูล  MySQL

12หน�จอของโปรแกรม phpMyAdmin ซงทำางานบน Web Browser

Page 13: PHP-5 ติดต่อฐานข้อมูล  MySQL

13หน�จอแสดง Database และ Table

Page 14: PHP-5 ติดต่อฐานข้อมูล  MySQL

14หนาจอแสดง ฟวสและจำานวนแถวของขอมล (Field,Record Column)

Page 15: PHP-5 ติดต่อฐานข้อมูล  MySQL

ฐานขอมล MYDATABASE

Fields Type Description

CustomerID

varchar(4)

รหสลกค�(PK)

Name varchar(50)

ชอ

Email varchar(50)

อเมล

CountryCode

varchar(2)

รหสประเทศ(FK)

Budget double งบประม�ณUsed double รวมจำ�นวน

เงนทใช

15

Fields Type Descri

ptionCountryCode

VARCHAR( 2 )

รหสประเทศ(PK)

CountryName

VARCHAR(50)

ชอประเทศ

Fields Type Description

AuditID int(4) รหส Audit(PK)

CustomerID

varchar(4)

รหสลกค�(FK)

Date date วนทUsed double จำ�นวนเงนท

ใช

Customer Audit

Country

Page 16: PHP-5 ติดต่อฐานข้อมูล  MySQL

MYSQL PHPMYADMIN CREATE DATABASE & TABLE เรยนรวธการสราง DATABASE และ TABLE ใน PHPMYADMIN เปด PHPMYADMIN ดวย

HTTP://LOCALHOST/PHPMYADMIN

16กรอก User/Password ในก�รเข�ส phpMyAdmin ปกตจะ Default เปน root/root

Page 17: PHP-5 ติดต่อฐานข้อมูล  MySQL

หน�จอของโปรแกรม phpMyAdmin ซงทำ�ง�นบน Web Browser

การ Create Database

17

ใสชอ Database Name และเลอก Create การ Create Table

ใสชอ Table และจำ�นวนฟวสทตองก�ร เลอก Go

Page 18: PHP-5 ติดต่อฐานข้อมูล  MySQL

18

ระบชอฟวส

Page 19: PHP-5 ติดต่อฐานข้อมูล  MySQL

ทดสอบสราง TABLE ในทนจะสราง TABLE ขนมา 3 TABLE คอ CUSTOMER,COUNTRY,AUDIT ตามรป TABLE CUSTOMER

19

Page 20: PHP-5 ติดต่อฐานข้อมูล  MySQL

20

Table country

Table audit

Page 21: PHP-5 ติดต่อฐานข้อมูล  MySQL

ขอมลใน Table

Rows Customer

21

Rows country Rows audit

Rows Customer

Page 22: PHP-5 ติดต่อฐานข้อมูล  MySQL

การ EXPORT DATABASE

22

เลอกท Export

ทำ�เพอนำ�ฐ�นขอมลทมอย MySQL ทสร�งเพอนำ�ไปใชทอนหรอ Backup เกบไว

Page 23: PHP-5 ติดต่อฐานข้อมูล  MySQL

เลอกรายการทตองการเพอ EXPORT ตาราง

23

เลอกตารางทตองการ

เลอกประเภทไฟล

เลอกรปแบบทตองการ

Page 24: PHP-5 ติดต่อฐานข้อมูล  MySQL

24

เลอกสงออกซงไมตองกำาหนดคาอยางใดครบ ปกตคา Default จะกำาหนดใหอยแลวครบ แลวอยาลมเลอก Save as file ดวยนะครบ เพราะเราจะสงออกเปน .sql

เลอก Go

เลอก Save ไฟล mydatabase.sql ซงเราจะนำาไฟลนไปใชงานหรอทำาการสำารองเกบไว

Page 25: PHP-5 ติดต่อฐานข้อมูล  MySQL

การ IMPORT DATABASE

25

หลงจากทเราได .sql มาแลวขนตอไปกคอการ Import เขาส Database ทใหมหรอทเดมครบ โดยใน Database ทใหมจะตองไมม Table ทไดทำาการ Export ออกตงแตแรก เพราะเราจะทำาการ Import เขาไปใหม

กรณทตองการสราง Database ใหม หรอหากมอยแลวกขามขนตอนไปไดเลยครบ

Page 26: PHP-5 ติดต่อฐานข้อมูล  MySQL

26เลอกท Import

Page 27: PHP-5 ติดต่อฐานข้อมูล  MySQL

27

เลอกไฟล .sql ทไดทำาการ Export ไวครบ และเลอกท Go

Page 28: PHP-5 ติดต่อฐานข้อมูล  MySQL

28Database ทไดทำาการ Import เขามา

Page 29: PHP-5 ติดต่อฐานข้อมูล  MySQL

MYSQL RELATION CASCADE ON DELETE/ON UPDATE

29

MySQL Relation CasCade On Delete/On Update ความสามารถของ MySQL ในการทำา Relation กสามารถทำาไดเชนเดยวกน โดยกำาหนดชนดของ Table เปนแบบ InnoDB โดยสามารถทำาการ CasCade DELETE หรอ UPDATE ได

Syntax InnoDBCREATE TABLE `customer` (....) ENGINE=InnoDB;

Page 30: PHP-5 ติดต่อฐานข้อมูล  MySQL

Table : customerCREATE TABLE `customer` (`CustomerID` varchar(4) NOT NULL,`Name` varchar(50) NOT NULL,`Email` varchar(50) NOT NULL,`CountryCode` varchar(2) NOT NULL,`Budget` double NOT NULL,`Used` double NOT NULL,PRIMARY KEY (`CustomerID`)) ENGINE=MyISAM;

INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);

INSERT INTO `customer` VALUES ('C002', 'John Smith', '[email protected]', 'EN', 2000000, 800000);

INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);

INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);

INSERT INTO `customer` VALUES ('C005', 'Weerachai Nukitram', '[email protected]', 'TH', 2000000, 100000);

30

ดตวอยางตวอยางไดทำาการสราง Table ชอ customer และ audit ซงเมอมการลบขอมลในตาราง customer ขอมลทอยใน audit จะถกลบไปดวย

Page 31: PHP-5 ติดต่อฐานข้อมูล  MySQL

Table : auditCREATE TABLE `audit` (`AuditID` int(4) NOT NULL,`CustomerID` varchar(4) NOT NULL,`Date` date NOT NULL,`Used` double NOT NULL,PRIMARY KEY (`AuditID`),FOREIGN KEY (CustomerID) REFERENCES customer (CustomerID) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;

INSERT INTO `audit` VALUES (1, 'C001', '2008-08-01', 100000);INSERT INTO `audit` VALUES (2, 'C001', '2008-08-05', 200000);INSERT INTO `audit` VALUES (3, 'C001', '2008-08-10', 300000);INSERT INTO `audit` VALUES (4, 'C002', '2008-08-02', 400000);INSERT INTO `audit` VALUES (5, 'C002', '2008-08-07', 100000);INSERT INTO `audit` VALUES (6, 'C002', '2008-08-15', 300000);INSERT INTO `audit` VALUES (7, 'C003', '2008-08-20', 400000);INSERT INTO `audit` VALUES (8, 'C003', '2008-08-25', 200000);INSERT INTO `audit` VALUES (9, 'C004', '2008-07-04', 100000);INSERT INTO `audit` VALUES (10, 'C005', '2008-07-04', 200000); 31

Page 32: PHP-5 ติดต่อฐานข้อมูล  MySQL

32

เพยงเทานกจะได Relation 2 ตาราง เมอมการแกไขหรอลบขอมลใน customer ขอมลใน audit จะมผลในการแกไข หรอ ลบเชนเดยวกน

Page 33: PHP-5 ติดต่อฐานข้อมูล  MySQL

SQL สำาหรบการสราง TABLE ชอ COUNTRYCREATE TABLE `country` ( `CountryCode` VARCHAR( 2 ) NOT

NULL , `CountryName` VARCHAR( 50 ) NOT NULL ,

PRIMARY KEY ( `CountryCode` )) ENGINE = MYISAM ;

INSERT INTO `country` VALUES ('TH', 'Thailand');INSERT INTO `country` VALUES ('EN', 'England');INSERT INTO `country` VALUES ('US', 'United State');

33

Page 34: PHP-5 ติดต่อฐานข้อมูล  MySQL

MYSQL DATA SOURCE CONFIG (DSN)

34

MySQL DSN Data Source ในภาษา Programming สวนใหญแลวจะม Driver สำาหรบไวตดตอกบ MySQL อยแลวครบ แตในบางภาษาจะไมสามารถเรยกใชงานไดโดยตรง และวธทใชกนสวนมากกคอการตดตอผาน ODBC ในการเรยกใชงาน Oracle ผาน ODBC จะตองทำาการ Set DSN Name กอนครบถงจะสามารถทำาการเรยก Database ใชงานได

ดาวโหลด MyODBC Driver ไดท www.mysql.com ในทนใชเวอรชน 5.1 เปนเวอรชนลาสดหรอดาวนโหลดจาก URL นไดโดยตรง

Download MyODBC 5.1 Driver http://www.thaicreate.com/upload/tutorial/mysql-connector-odbc-5.1.5-win32.zip

ตวอยางการตดตง MyODBC

MyODBC http://www.thaicreate.com/asp/asp-mysql-connect-database.html

เรมการ Config DSN

ไปทเมน Start -> Setting -> Control Panel -> Administrative Tools

Page 35: PHP-5 ติดต่อฐานข้อมูล  MySQL

35

โปรแกรม ODBC

เลอกท Tab System DSN -> Add

วธการนจะตองมการลง MySQL ODBC 5.1 เสยกอน

Page 36: PHP-5 ติดต่อฐานข้อมูล  MySQL

36เลอกท MySQL ODBC 5.1 Driver -> Finish

Page 37: PHP-5 ติดต่อฐานข้อมูล  MySQL

37กำาหนดชอ DSN ในทนผมกำาหนดเปน mydatabase และเลอกฐานขอมล mydatabase

Page 38: PHP-5 ติดต่อฐานข้อมูล  MySQL

38

ทดสอบ Connection คลกท Test และโปรแกรมแสดงสถานะการตดตอ

DSN ทไดจากการสราง

Page 39: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL CONNECT TO DATABASE

39

PHP MySQL Connect to Database สำาหรบการเขยน PHP กบ MySQL นนถอเปนปจจยหลกและไดรบความนยมมากทสดในบรรดา Database ทงหลายครบ เพราะนอกจากฟรทง 2 ตวแลว function ตาง ๆ PHP ไดออกแบบมาใหใชกบ MySQL Database โดยเฉพาะครบ

จดเดนของ PHP และ MySQL คอ MySQL สามารถทงานไดอยางรวดเรว และคอนขางจะเกบขอมลไดหลาย Record

mysql_error()Fatal error: Call to undefined function mysql_connect() in C:\AppServ\www\myphp\phpMySQLConnect.php on line 7

Page 40: PHP-5 ติดต่อฐานข้อมูล  MySQL

ถ�เกดขอผดพล�ดต�มทยกตวอย�งใหใหทำ�ก�รด�วนโหลดตว extension ชอ php_mysql.dllphp_mysql.dll

http://www.thaicreate.com/php/php_mysql.dll.html

ไปไวในโฟเดอร extension ของ php เชน C:\AppServ\php5\ext หรอส�ม�รถตรวจสอบ Path ไดท php.iniStart -> Run -> php.iniextension_dir = "C:/AppServ\php5\ext".....extension=php_mysql.dll

และก Restart Apache หรอ Web Server ดวยนะครบ เสรจแลวเปนอนใชได

Syntax$objConnect = mysql_connect($host,$user,$pass);$host = server,ip,computer-name$user = user$pass = password

40

Page 41: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root");if($objConnect){echo "Database Connected.";}else{echo "Database Connect Failed.";}mysql_close($objConnect);?></body></html> 41

ตวอยางการเขยนตดตอ PHP กบ MySQL phpMySQLConnect.php

Page 43: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL LIST TABLE PROPERTIES (แสดงชอฟลด)

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");$intNumField = mysql_num_fields($objQuery);$i = 0;echo "<b>Table customer have $intNumField Fields.</b><br>";for($i=1;$i<$intNumField;$i++){echo $i."=".mysql_field_name($objQuery,$i)." (".mysql_field_type($objQuery,$i).")<br>"; }mysql_close($objConnect);?></body></html>

43

PHP MySQL List Table Properties ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL เพอแสดงคณสมบตของ Table ตวอย�ง ListField.php

Page 45: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL LIST RECORD (แสดงตาราง)

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");?><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th></tr><?

45

PHP MySQL List Record ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL เพอดงขอมลจ�ก Table ม�แสดง ตวอยาง

ListRecord.php

สามารเปนเปน Query อนๆไดตามตองการ เชน

SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;

SELECT * FROM customer inner join country on

customer.countrycode= country.countrycode

Page 46: PHP-5 ติดต่อฐานข้อมูล  MySQL

while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td></tr><? } ?></table><?mysql_close($objConnect);?></body></html>

46

ListRecord.php ตอ

Page 48: PHP-5 ติดต่อฐานข้อมูล  MySQL

ถาเพมเตมการโดยการทำา Link เพอเรยงตามตวอกษรในคอลมภตางๆ<html><head></head><body><? $objConnect = mysql_connect("localhost","root",“root") or

die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");

IF($M=="") { $strSQL = "SELECT * FROM customer";} elseif($M==1) { $strSQL = "SELECT * FROM customer order by CustomerID Asc";} elseif($M==2) { $strSQL = "SELECT * FROM customer order by Name Asc";} elseif($M==3) { $strSQL = "SELECT * FROM customer order by Email Asc";} elseif($M==4) { $strSQL = "SELECT * FROM customer order by CountryCode Asc";} elseif($M==5) { $strSQL = "SELECT * FROM customer order by Budget Asc";} elseif($M==6) { $strSQL = "SELECT * FROM customer order by Used Asc";}

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");?>

ListSort.php

Page 49: PHP-5 ติดต่อฐานข้อมูล  MySQL

<table width="800" border="1"><tr><th width="91"> <div align="center"> <a href="ListSort.php?M=1">CustomerID </a>

</div></th><th width="100"> <div align="center"> <a href="ListSort.php?M=2">Name</a> </div></th><th width="198"> <div align="center"> <a href="ListSort.php?M=3">Email </a> </div></th><th width="97"> <div align="center"> <a href="ListSortphp?M=4">CountryCode</a>

</div></th><th width="59"> <div align="center"> <a href="ListSort.php?M=5">Budget </a> </div></th><th width="71"> <div align="center"> <a href="ListSort.php?M=6">Used</a> </div></th></tr>

ListSort.php ตอ

Page 50: PHP-5 ติดต่อฐานข้อมูล  MySQL

<? $i=0;while($objResult = mysql_fetch_array($objQuery)){

$i++; if ($i%2==0){ echo "<tr bgcolor=#FFFF99>";}else{ echo "<tr bgcolor=#FFCCCC>"; }?><td><div

align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div

align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td>

</tr><? } ?></table><? mysql_close($objConnect); ?></body></html>

ListSort.php ตอ

Page 52: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL RANDOM RECORD สมรายการเพอแสดง<html>

<head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer ORDER BY RAND() LIMIT 2 ";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");?><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th></tr><?

52

ตวอยาง RandomRecord.php

Page 53: PHP-5 ติดต่อฐานข้อมูล  MySQL

while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td></tr><?}?></table><?mysql_close($objConnect);?></body></html> 53

RandomRecord.php ตอ

Page 55: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL LIST RECORD PAGING แสดงรายการเปนหนา

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer ";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");$Num_Rows = mysql_num_rows($objQuery);$Per_Page = 2; // Per Page$Page = $_GET["Page"];if(!$_GET["Page"]){$Page=1;} 55

PHP MySQL List Record Paging ตวอยางนจะเปนการเขยนโปรแกรม PHP กบ MySQL เพอดงขอมลจาก Table มาแสดงและมการแบงการแสดงผลเปนหนา

ListRecordPaging.php

Page 56: PHP-5 ติดต่อฐานข้อมูล  MySQL

$Prev_Page = $Page-1;$Next_Page = $Page+1;$Page_Start = (($Per_Page*$Page)-$Per_Page);if($Num_Rows<=$Per_Page){$Num_Pages =1;}else if(($Num_Rows % $Per_Page)==0){$Num_Pages =($Num_Rows/$Per_Page) ;}else{$Num_Pages =($Num_Rows/$Per_Page)+1;$Num_Pages = (int)$Num_Pages;}$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";$objQuery = mysql_query($strSQL);?><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th></tr><?

56

ListRecordPaging.php ตอ

Page 57: PHP-5 ติดต่อฐานข้อมูล  MySQL

while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td></tr><?}?></table><br>Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :<?if($Prev_Page){echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";} 57

Page 58: PHP-5 ติดต่อฐานข้อมูล  MySQL

for($i=1; $i<=$Num_Pages; $i++){if($i != $Page){ echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";}else{echo "<b> $i </b>";}}if($Page!=$Num_Pages){echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";}mysql_close($objConnect);?></body></html>

58

Page 60: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL SEARCH RECORD คนหารายการ

<html><head><title>iamsanya.com</title></head><body><form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>"><table width="599" border="1"><tr><th>Keyword<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>"><input type="submit" value="Search"></th></tr></table></form><?

60

PHP MySQL Search Record ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL เพอคนห�ขอมลจ�ก Table ตวอยาง SearchRecord.php

Page 61: PHP-5 ติดต่อฐานข้อมูล  MySQL

if($_GET["txtKeyword"] != ""){$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");// Search By Name or Email$strSQL = "SELECT * FROM customer WHERE (Name LIKE '%".$_GET["txtKeyword"]."%' or Email LIKE '%".$_GET["txtKeyword"]."%' )";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");?><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th></tr><? 61

SearchRecord.php ตอ

Page 62: PHP-5 ติดต่อฐานข้อมูล  MySQL

while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td></tr><?}?></table><?mysql_close($objConnect);}?></body></html>

62

SearchRecord.php ตอ

Page 64: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL SEARCH RECORD PAGING คนหาแสดงเปนหนา

<html><head><title>iamsanya.com</title></head><body><form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>"><table width="599" border="1"><tr><th>Keyword<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>"><input type="submit" value="Search"></th></tr></table></form><?

64

PHP MySQL Search Record Paging ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL เพอคนห�ขอมลจ�ก Table ม�แสดงและมก�รแบงก�รแสดงผลเปนหน�

ตวอยาง SearchRecordPaging.php

Page 65: PHP-5 ติดต่อฐานข้อมูล  MySQL

if($_GET["txtKeyword"] != ""){$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");// Search By Name or Email$strSQL = "SELECT * FROM customer WHERE (Name LIKE '%".$_GET["txtKeyword"]."%' or Email LIKE '%".$_GET["txtKeyword"]."%')";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");$Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 2; // Per Page$Page = $_GET["Page"];if(!$_GET["Page"]){$Page=1;}$Prev_Page = $Page-1;$Next_Page = $Page+1; 65

SearchRecordPaging.php ตอ

Page 66: PHP-5 ติดต่อฐานข้อมูล  MySQL

$Page_Start = (($Per_Page*$Page)-$Per_Page);if($Num_Rows<=$Per_Page){$Num_Pages =1;}else if(($Num_Rows % $Per_Page)==0){$Num_Pages =($Num_Rows/$Per_Page) ;}else{$Num_Pages =($Num_Rows/$Per_Page)+1;$Num_Pages = (int)$Num_Pages;}

$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";$objQuery = mysql_query($strSQL); ?><table width="600" border="1">

66

SearchRecordPaging.php ตอ

Page 67: PHP-5 ติดต่อฐานข้อมูล  MySQL

<tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th></tr><?while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td></tr><?}?> 67

SearchRecordPaging.php ตอ

Page 68: PHP-5 ติดต่อฐานข้อมูล  MySQL

</table><br>Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :<?if($Prev_Page){echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";}for($i=1; $i<=$Num_Pages; $i++){if($i != $Page){echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";}else{echo "<b> $i </b>";}}if($Page!=$Num_Pages){echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";}mysql_close($objConnect);} ?></body></html> 68

SearchRecordPaging.php ตอ

Page 70: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL ADD/INSERT RECORD การเพมขอมล

<html><head><title>iamsanya.com</title></head><body><form action="AddSave.php" name="frmAdd" method="post"><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="160"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th>

70

PHP MySQL Add/Insert Record ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL เพอเพมขอมลลงใน Table

ตวอยาง AddForm.php

Page 71: PHP-5 ติดต่อฐานข้อมูล  MySQL

<th width="70"> <div align="center">Budget </div></th><th width="70"> <div align="center">Used </div></th></tr><tr><td><div align="center"><input type="text" name="txtCustomerID" size="5"></div></td><td><input type="text" name="txtName" size="20"></td><td><input type="text" name="txtEmail" size="20"></td><td><div align="center"><input type="text" name="txtCountryCode" size="2"></div></td><td align="right"><input type="text" name="txtBudget" size="5"></td><td align="right"><input type="text" name="txtUsed" size="5"></td></tr></table><input type="submit" name="submit" value="submit"></form></body></html>

71

AddForm.php ตอ

Page 73: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "INSERT INTO customer ";$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";$strSQL .="VALUES ";$strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";$strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";$objQuery = mysql_query($strSQL);if($objQuery){echo "Save Done.";}else{echo "Error Save [".$strSQL."]";}mysql_close($objConnect);?></body></html>

73

AddSave.php

Page 75: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL CHECK ALREADY EXIST ADD/INSERT RECORD การตรวจขอมลซำากอนเพม

<html><head><title>iamsanya.com</title></head><body><form action="CheckExistRecordAddSave.php" name="frmAdd" method="post"><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="160"> <div align="center">Name </div></th> 75

PHP MySQL Check Already Exist Add/Insert Record ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL ตรวจสอบขอมลกอนเพมขอมลลงใน Table

ตวอยางCheckExistRecordAddForm.php

Page 76: PHP-5 ติดต่อฐานข้อมูล  MySQL

<th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="70"> <div align="center">Budget </div></th><th width="70"> <div align="center">Used </div></th></tr><tr><td><div align="center"><input type="text" name="txtCustomerID" size="5"></div></td><td><input type="text" name="txtName" size="20"></td><td><input type="text" name="txtEmail" size="20"></td><td><div align="center"><input type="text" name="txtCountryCode" size="2"></div></td><td align="right"><input type="text" name="txtBudget" size="5"></td><td align="right"><input type="text" name="txtUsed" size="5"></td></tr></table><input type="submit" name="submit" value="submit"></form></body></html> 76

CheckExistRecordAddForm.php ตอ

Page 78: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer WHERE CustomerID = '".$_POST["txtCustomerID"]."' ";$objQuery = mysql_query($strSQL);$objResult = mysql_fetch_array($objQuery);if($objResult){echo "CustomerID already exist.";}else{

78

CheckExistRecordAddSave.php

Page 79: PHP-5 ติดต่อฐานข้อมูล  MySQL

$strSQL = "";$strSQL = "INSERT INTO customer ";$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";$strSQL .="VALUES ";$strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";$strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";$objQuery = mysql_query($strSQL);if($objQuery){ echo "Save Done.";}else{ echo "Error Save [".$strSQL."]";}} mysql_close($objConnect);?></body></html>

79

CheckExistRecordAddSave.php ตอ

Page 81: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL EDIT/UPDATE RECORD แกไขขอมล

PHP MySQL Edit/Update Record ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL เพอแกไขขอมลลงใน Table

81

EditRecordList.php

EditRecordForm.php

EditRecordSave.php บนทกลงฐ�นขอมล

xxxxxxxxx

Form แกไข

xxx xxx แกไข

xxx xxx แกไข

ดงขอมลทเลอกม�แกไข

Page 82: PHP-5 ติดต่อฐานข้อมูล  MySQL

ตวอยาง

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer"; $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");?><table width="600" border="1"> 82

EditRecordList.php

Page 83: PHP-5 ติดต่อฐานข้อมูล  MySQL

<tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th><th width="30"> <div align="center">Edit </div></th></tr><?while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td> 83

EditRecordList.php ตอ

Page 84: PHP-5 ติดต่อฐานข้อมูล  MySQL

<td align="center"><a href="EditRecordForm.php?CusID=<?=$objResult["CustomerID"];?>">Edit</a></td></tr><?}?></table><?mysql_close($objConnect);?></body></html>

84

EditRecordList.php ตอ

Page 85: PHP-5 ติดต่อฐานข้อมูล  MySQL

85

Output

คลกไปทไฟล EditRecordForm.php ต�ม ID ทชอย

Page 86: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><form action="EditRecordSave.php?CusID=<?=$_GET["CusID"];?>" name="frmEdit" method="post"><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$strSQL = "SELECT * FROM customer WHERE CustomerID = '".$_GET["CusID"]."' ";$objDB = mysql_select_db("mydatabase");$objQuery = mysql_query($strSQL);$objResult = mysql_fetch_array($objQuery);if(!$objResult){ echo "Not found CustomerID=".$_GET["CusID"];}else{?><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="160"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="70"> <div align="center">Budget </div></th>

86

EditRecordForm.php

Page 87: PHP-5 ติดต่อฐานข้อมูล  MySQL

<th width="70"> <div align="center">Used </div></th></tr><tr><td><div align="center"><input type="text" name="txtCustomerID" size="5" value="<?=$objResult["CustomerID"];?>"></div></td><td><input type="text" name="txtName" size="20" value="<?=$objResult["Name"];?>"></td><td><input type="text" name="txtEmail" size="20" value="<?=$objResult["Email"];?>"></td><td><div align="center"><input type="text" name="txtCountryCode" size="2" value="<?=$objResult["CountryCode"];?>"></div></td><td align="right"><input type="text" name="txtBudget" size="5" value="<?=$objResult["Budget"];?>"></td><td align="right"><input type="text" name="txtUsed" size="5" value="<?=$objResult["Used"];?>"></td></tr></table><input type="submit" name="submit" value="submit"><?}mysql_close($objConnect);?></form></body></html>

87

EditRecordForm.php ตอ

Page 89: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "UPDATE customer SET ";$strSQL .="CustomerID = '".$_POST["txtCustomerID"]."' ";$strSQL .=",Name = '".$_POST["txtName"]."' ";$strSQL .=",Email = '".$_POST["txtEmail"]."' ";$strSQL .=",CountryCode = '".$_POST["txtCountryCode"]."' ";$strSQL .=",Budget = '".$_POST["txtBudget"]."' ";$strSQL .=",Used = '".$_POST["txtUsed"]."' ";$strSQL .="WHERE CustomerID = '".$_GET["CusID"]."' ";$objQuery = mysql_query($strSQL);if($objQuery){ echo "Save Done.";}else{echo "Error Save [".$strSQL."]";}mysql_close($objConnect);?></body></html>

89

EditRecordSave.php

Page 90: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MYSQL DELETE RECORD การลบขอมล PHP MySQL Delete Record ตวอย�งนจะเปนก�รเขยนโปรแกรม

PHP กบ MySQL เพอลบขอมลใน Table

90

DeleteRecordList.php <html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "SELECT * FROM customer";$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");?><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="98"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="59"> <div align="center">Budget </div></th><th width="71"> <div align="center">Used </div></th><th width="30"> <div align="center">Delete </div></th></tr>

Page 91: PHP-5 ติดต่อฐานข้อมูล  MySQL

<?while($objResult = mysql_fetch_array($objQuery)){?><tr><td><div align="center"><?=$objResult["CustomerID"];?></div></td><td><?=$objResult["Name"];?></td><td><?=$objResult["Email"];?></td><td><div align="center"><?=$objResult["CountryCode"];?></div></td><td align="right"><?=$objResult["Budget"];?></td><td align="right"><?=$objResult["Used"];?></td><td align="center">

<a href="DeleteRecord.php?CusID=<?=$objResult["CustomerID"];?>"> Delete</a>

</td> </tr><?}?></table><?mysql_close($objConnect);?></body></html>

91

DeleteRecordList.php ตอ

Page 92: PHP-5 ติดต่อฐานข้อมูล  MySQL

OUTPUT

92คลกไปทไฟล DeleteRecord.php ต�ม ID ทชอย

Page 93: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");$strSQL = "DELETE FROM customer ";$strSQL .="WHERE CustomerID = '".$_GET["CusID"]."' ";$objQuery = mysql_query($strSQL);if($objQuery){ echo "Record Deleted.";}else{echo "Error Delete [".$strSQL."]";}mysql_close($objConnect);?></body></html>

93

DeleteRecord.php

Page 94: PHP-5 ติดต่อฐานข้อมูล  MySQL

PHP MySQL Transaction (Begin,Commit,Rollback) ตวอย�งนจะเปนก�รเขยนโปรแกรม PHP กบ MySQL ใชง�น Transaction ในก�รตรวจสอบความถกตองกอนก�รทำ�ก�รบนทกขอมลจรง หลกก�รคอ เมอมก�ร Begin แลว ภ�ยใตคำ�สงทอยในเงอนไนนจะมก�รตรวจสอบว�มก�รทำ�ง�นถกตอง หรอมขอผดพล�ดหรอไม ถ�ไมมขอผดพล�ดกจะทำ�ก�ร Commit หรอถ�มขอผดพล�ดโปรแกรมจะทำ�ก�ร Rollback คอยกเลกขอมลทไดทำ�ก�รกระทำ�ในแตแรก

94

Syntax//*** Start Transaction ***//mysql_query("BEGIN");

//*** Commit Transaction ***//mysql_query("COMMIT")

//*** RollBack Tranasction ***//mysql_query("ROLLBACK")

ในก�รใชง�น Transaction จะตองกำ�หนดชนดของ Table เปนแบบ InnoDB

InnoDB SyntaxCREATE TABLE `customer` (....) ENGINE=InnoDB;

Page 95: PHP-5 ติดต่อฐานข้อมูล  MySQL

กรณมขอผดพลาด MYSQL SAID: DOCUMENTATION #1289 - The 'InnoDB' feature is disabled; you

need MySQL built with 'InnoDB' to have it working

ใหทำ�ก�รเปดไฟล my.ini (C:\AppServ\MySQL\my.ini)

skip-innodbแกเปน #skip-innodb### Restart MySQL Service ###

95

Page 96: PHP-5 ติดต่อฐานข้อมูล  MySQL

ตวอยางตวอย�งนจะเปนก�รเพมขอมล ไดสมมตก�รเพมขอมลซำ� ซงม Primary Key ชอ CustomerID ซงจะส�ม�รถเพมขอมลสำ�เรจใน Statement แรก และ Statement ท 2 จะไมส�ม�รถเพมขอมลได และเมอมก�ร RollBack ขอมล Statement แรกทถก Insert ไปแลวกจะถกยกเลกในทนท สำ�หรบก�รใช Transaction ส�ม�รถใชไดทงก�ร Insert/Update/Delete Record

96

Page 97: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><form action="TransactionAddSave.php" name="frmAdd" method="post"><table width="600" border="1"><tr><th width="91"> <div align="center">CustomerID </div></th><th width="160"> <div align="center">Name </div></th><th width="198"> <div align="center">Email </div></th><th width="97"> <div align="center">CountryCode </div></th><th width="70"> <div align="center">Budget </div></th><th width="70"> <div align="center">Used </div></th></tr><tr><td><div align="center"><input type="text" name="txtCustomerID" size="5"></div></td><td><input type="text" name="txtName" size="20"></td><td><input type="text" name="txtEmail" size="20"></td><td><div align="center"><input type="text" name="txtCountryCode" size="2"></div></td><td align="right"><input type="text" name="txtBudget" size="5"></td><td align="right"><input type="text" name="txtUsed" size="5"></td></tr></table><input type="submit" name="submit" value="submit"></form></body></html>

97

TransactionAddForm.php

Page 99: PHP-5 ติดต่อฐานข้อมูล  MySQL

<html><head><title>iamsanya.com</title></head><body><?$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");$objDB = mysql_select_db("mydatabase");//*** Start Transaction ***//mysql_query("BEGIN");//*** Query 1 ***//$strSQL = "INSERT INTO customer ";$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";$strSQL .="VALUES ";$strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";$strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";$objQuery1 = mysql_query($strSQL);

99

TransactionAddSave.php

Page 100: PHP-5 ติดต่อฐานข้อมูล  MySQL

$strSQL = "INSERT INTO customer ";$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";$strSQL .="VALUES ";$strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";$strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";$objQuery2 = mysql_query($strSQL);if(($objQuery1) and ($objQuery2)){//*** Commit Transaction ***//mysql_query("COMMIT");echo "Save Done.";}else{//*** RollBack Transaction ***//mysql_query("ROLLBACK");echo "Error Save [".$strSQL."]";}mysql_close($objConnect);?></body></html> 100

TransactionAddSave.php ตอ

Error Message Error Save [INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) VALUES ('C005','Weerachai Nukitram','[email protected]' ,'TH','2000000','100000') ]

Page 101: PHP-5 ติดต่อฐานข้อมูล  MySQL

อางอง www.thaicreate.com

101