100
PHP-5 ตตตตตตตตตตตตตตต MYSQL 1

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

Embed Size (px)

DESCRIPTION

PHP-5 ติดต่อฐานข้อมูล MySQL. - PowerPoint PPT Presentation

Citation preview

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

PHP-5ติ�ดติ�อฐานข้อมู�ล MYS

QL 1

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

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

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

ป็5จัจั1บ�นบรู้�ษ�ทำซ�นไมโครู้ซ�สเต์6มส& (Sun Microsystems, Inc. )เข้�ซ4.อก�จัก�รู้ข้อง MySQL AB เรู้+ยบรู้อยแลว ฉะน�.นผล�ต์ภ�ณฑ์&ภ�ยใต์ MySQL AB ทำ�.งห้มดจัะต์กเป็�นข้องซ�น

2

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

รุ่��นข้องผล�ติภั�ณฑ์�

รู้1 "นข้องผล�ต์ภ�ณฑ์&น�.นแบ"งออกม�ไดส�มส�ยก�รู้ผล�ต์ ไดแก" เวอรู้&ช้�นใช้ฟรู้+ เวอรู้&ช้�นก�รู้ค� และเวอรู้&ช้�นทำ+)สน�บสน1นก�บผล�ต์ภ�ณฑ์& SAP (MAX DB) คว�มแต์กต์"�งค4อเวอรู้&ช้�นคอมม�วน�ต์+.น� .นส�ม�รู้ถน��ไป็ใช้ง�นไดฟรู้+แต์"ข้�ดก�รู้สน�บ สน1นห้รู้4อก�รู้ช้"วยเห้ล4อเม4)อม+ป็5ญห้�เก�ดข้2.น , เวอรู้&ช้�นทำ+)เป็�นคอมเมอรู้&เช้+ยลน�.นให้บรู้�ก�รู้ด�นคว�มสน�บสน1นเม4)อม+ป็5ญห้� (ซ4.อบรู้�ก�รู้ ) สรู้1ป็ครู้"�วๆ ป็รู้ะเภทำด�ต์�เบสให้เล4อกใช้ด�งน+.

* MySQL เอนเทำอรู้&ไพรู้ส& Enterprise* MySQL คล�สเต์อรู้& Cluster* MySQL Embedded* MySQL Community (opensource เวอรู้&ช้�น )

3

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

ความูสามูารุ่ถที่��เพิ่��มูข้!"น

ในเวอรู้&ช้�น 50. ม+คว�มส�ม�รู้ถห้ล�ยอย"�งทำ+)ส��ค�ญส��ห้รู้�บรู้ะบบฐ�นข้อมลข้น�ดให้ญ"ห้รู้4อรู้ะด�บองค&กรู้ (EnterPrise Feature) เช้"น Store Procedure, database trigger, database view, database schema ซ2)งไดม+ก�รู้ป็รู้�บเพ�)มป็รู้ะส�ทำธุ�ภ�พในส"วนข้องต์�รู้�งและก�รู้ทำ��ด�ช้น+ (index) ข้2.นม�อ+ก ป็5จัจั1บ�นเวอรู้&ช้�น community ห้รู้4อเวอรู้&ช้�นทำ+)เสถ+ยรู้ (stable) 5.0 และเวอรู้&ช้�นทำดสอบค4อ 5.1 beta release และ 5.2 Alpha

ต์�.งแต์"เวอรู้&ช้�น 51 เรู้�)มสน�บสน1นก�รู้ทำ�� Parttion Database , ต์�รู้�งเวล�ส��ห้รู้�บเห้ต์1ก�รู้ณ&ต์"�งๆ (Event schedule)

4

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

ปรุ่ะเภัที่การุ่จั�ดเก'บข้อมู�ล (Database Storage Engine) ที่��สน�บสน�น

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

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

ชน�ดข้องข้อมู�ลที่��สน�บสน�น

ช้น�ดข้อมลทำ+) MySQL สน�บสน1นแบ"งเป็�นส�มป็รู้ะเภทำห้ล�กให้ญ"ๆ

ชน�ดข้อมู�ลที่��เป*นติ�วเลข้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 เป็�นทำ+)น�ยมใช้ก�นม�กส��ห้รู้�บฐ�นข้อมลส��ห้รู้�บเว6บไซต์& เช้"น ม+เด+ยว�ก� และ phpBB และน�ยมใช้ง�นรู้"วมก�บภ�ษ�โป็รู้แกรู้ม PHP ซ2)งม�กจัะไดช้4)อว"�เป็�นค" จัะเห้6นไดจั�กค"ม4อคอมพ�วเต์อรู้&ต์"�งๆ ทำ+)จัะสอนก�รู้ใช้ง�น MySQL และ PHP ควบค"ก�นไป็ นอกจั�กน+. ห้ล�ยภ�ษ�โป็รู้แกรู้มทำ+)ส�ม�รู้ถทำ��ง�นรู้"วมก�บฐ�นข้อมล MySQL ซ2)งรู้วมถ2ง ภ�ษ�ซ+ ซ+พล�สพล�ส ป็�สค�ล ซ+ช้�รู้&ป็ ภ�ษ�จั�ว� ภ�ษ�เพ�รู้&ล พ+เอช้พ+ ไพทำอน รู้บ+ และภ�ษ�อ4)น ใช้ง�นผ"�น API ส��ห้รู้�บโป็รู้แกรู้มทำ+)ต์�ดต์"อผ"�น ODBC ห้รู้4อ ส"วนเช้4)อมต์"อก�บภ�ษ�อ4)น (database connector) เช้"น เอเอสพ+ ส�ม�รู้ถเรู้+ยกใช้ MySQL ผ"�นทำ�ง MyODBC,ADO,ADO.NET เป็�นต์น

โปรุ่แกรุ่มูช�วยในการุ่จั�ดการุ่ฐานข้อมู�ล และ ที่0างานก�บฐานข้อมู�ล

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

8

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

ส�วนเช1�อมูติ�อก�บภัาษาการุ่พิ่�ฒนาอ1�น (database connector)

ม+ส"วนต์�ดต์"อ (interface) เพ4)อเช้4)อมต์"อก�บภ�ษ�ในก�รู้พ�ฒน� อ4)นๆ เพ4)อให้เข้�ถ2งฟ5งก&ช้�นก�รู้ทำ��ง�นก�บฐ�นข้อมล MySQL ไดเช้"น ODBC (Open Database Connector) อ�นเป็�นม�ต์รู้ฐ�นกล�งทำ+)ก��ห้นดม�เพ4)อให้ใช้เป็�นสะพ�นในก�รู้เช้4)อมต์"อก�บโป็รู้แกรู้ม ห้รู้4อรู้ะบบอ4)นๆ เช้"น MyODBC อ�นเป็�นไดรู้เวอรู้&เพ4)อใช้ส��ห้รู้�บก�รู้เช้4)อมต์"อในรู้ะบบป็ฏิ�บ�ต์�ก�รู้ว�นโดว&, JDBC คล�สส"วนเช้4)อมต์"อส��ห้รู้�บ Java เพ4)อใช้ในก�รู้ต์�ดต์"อก�บ MySQL และม+ API (Application Programming Interface) ต์"�งๆม+ให้เล4อกใช้ม�กม�ยในก�รู้ทำ+)เข้�ถ2ง MySQL โดยไม"ข้2.นอย"ก�บภ�ษ�ก�รู้พ�ฒน�ใดภ�ษ�ห้น2)งนอกเห้น4อจั�ก ต์�วเช้4)อมต์"อก�บภ�ษ�อ4)น (Connector) ทำ+)ไดกล"�วม�แลว ย�งม+ API ทำ+)สน�บสน1นในข้ณะน+.ค4อ* DBI ส��ห้รู้�บก�รู้เช้4)อมต์"อก�บ ภ�ษ� perl* Ruby ส��ห้รู้�บก�รู้เช้4)อมต์"อก�บ ภ�ษ� ruby* Python ส��ห้รู้�บก�รู้เช้4)อมต์"อก�บภ�ษ� python* .NET ส��ห้รู้�บก�รู้เช้4)อมก�บภ�ษ� .NET framework* MySQL++ ส��ห้รู้�บเช้4)อมต์"อก�บภ�ษ� C++* Ch ส��ห้รู้�บก�รู้เช้4)อมต์"อก�บ Ch (C/C++ interpreter)

ย�งม+โป็รู้แกรู้มอ+กต์�ว เป็�นโป็รู้แกรู้มบรู้�ห้�รู้พ�ฒน�โดยผอ4)น ซ2)งใช้ก�นอย"�งแพรู้"ห้ล�ยและน�ยมก�นเข้+ยนในภ�ษ�พ+เอช้พ+ เป็�นโป็รู้แกรู้มเว6บแอป็พล�เคช้�น ช้4)อ phpMyAdmin 9

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

MYSQL INSTALLATION ติ�ดติ�"ง MYSQL หา MySQL ไดจัากไหน??

MySQL เป*น Software ทำ+)ส�ม�รู้ถใช้ง�นไดฟรุ่� โดยห้�โห้ลดไดจั�กเว6บไซต์&

http://www.mysql.com

ห้รู้4อส��ห้รู้�บผทำ+)เข้+ยน PHP อย"แลว ส�ม�รู้ถต์�ดต์�.งต์�ง Appserv ไดเลยครู้�บ

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

ห้รู้4อห้�กผทำ+)พ�ฒน�โป็รู้แกรู้มดวยภ�ษ� ASP หรุ่1อ ASP.NET แลวใช้ IISเป*น Web Server ถ�ห้�กต์�ดต์�.ง Appserv ผมแนะน��ให้เป็ล+)ยน Portข้อง Apache ใน Appserv เป*น 81 ห้รู้4ออย"�งอ4)นทำ+)ไม"ใช้" 80

เพ4)อป็@องก�นไม"ให้ม+ผลกรู้ะทำบก�บ IIS และ Apache Web Server

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

10

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

phpMyAdmin ทำ��คว�มรู้ จั�ก phpMyAdmin???

phpMyAdmin ค1ออะไรุ่ ?phpMyAdmin เป็�นโป็รู้แกรู้มทำ+)ถกพ�ฒน�ดวย PHP ที่��ใชจั�ดการุ่ MySQL ผ"�น Web Browser โดยส�ม�รู้ถจั�ดก�รู้

MySQL Database เก4อบทำ1กอย"�ง เช้"นก�รู้ CreateDatabase/Table/Field เพ�)ม/ลบ/แกไข้ ข้อมลห้รู้4อโครู้งสรู้�งได

phpMyAdmin หาไดจัากไหน ?ห้�กผทำ+)ต์�ดต์�.งโป็รู้แกรู้ม Appserv อย"แลวส�ม�รู้ถเรู้+ยกใช้ phpMyAdmin ไดทำ�นทำ+ http://localhost/phpMyAdmin

phpMyAdmin มูาก�บ Package ข้อง Appserv

Appserv & phpMyAdmin - -http://www.thaicreate.com/php/php install

apache.html

หรุ่1อจัะหาดาวน�โหลด phpMyAdmin ไดจัากเว'บไซติ� Download phpMyAdmin

http://www.phpmyadmin.net/11

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

12ห้น�จัอข้องโป็รู้แกรู้ม phpMyAdmin ซ!�งที่0างานบน Web Browser

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

13

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

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

14

หนาจัอแสดง ฟ7วส�และจั0านวนแถวข้องข้อมู�ล (Field,Record Column)

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

ฐานข้อมู�ล MYDATABASE

Fields TypeDescrip

tionCustomerID

varchar(4)

รู้ห้�สลกค�(PK)

Name varchar(50)

ช้4)อ

Email varchar(50)

อ+เมล&

CountryCode

varchar(2)

รู้ห้�สป็รู้ะเทำศ(FK)

Budget double งบป็รู้ะม�ณUsed double รู้วมจั��นวน

เง�นทำ+)ใช้15

Fields

TypeDescription

CountryCode

VARCHAR( 2 )

รู้ห้�สป็รู้ะเทำศ(PK)

CountryName

VARCHAR(50)

ช้4)อป็รู้ะเทำศ

Fields TypeDescription

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 เป7ด PHPMYADMIN ดวย

HTTP://LOCALHOST/PHPMYADMIN

16กรู้อก User/Password ในก�รู้เข้�ส" phpMyAdmin ป็กต์�จัะ Default เป็�น root/root

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

ห้น�จัอข้องโป็รู้แกรู้ม phpMyAdmin ซ2)งทำ��ง�นบน Web Browser

การุ่ Create Database

17

ใส"ช้4)อ ใส"ช้4)อ Database NameDatabase Name และเล4อก และเล4อก CreateCreate

การุ่ การุ่ Create Create TableTable

ใส"ช้4)อ ใส"ช้4)อ TableTable และจั��นวนฟ3วส&ทำ+)ต์องก�รู้ เล4อกและจั��นวนฟ3วส&ทำ+)ต์องก�รู้ เล4อก GoGo

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

18

รุ่ะบ�ช1�อฟ7วส�รุ่ะบ�ช1�อฟ7วส�

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

ที่ดสอบสรุ่าง TABLE ในที่��น��จัะสรุ่าง TABLE ข้!"นมูา 3 TABLE ค1อ CUSTOMER,COUNTRY,AUDIT ติามูรุ่�ป TABLE CUSTOMER

19

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

20

Table countryTable country

Table audit 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

เล4อกทำ+) เล4อกทำ+) ExportExport

ทำ��เพ4)อน��ฐ�นข้อมลทำ+)ม+อย" MySQL ทำ+)สรู้�งเพ4)อน��ไป็ใช้ทำ+)อ4)นห้รู้4อ Backup เก6บไว

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

เล1อกรุ่ายการุ่ที่��ติองการุ่เพิ่1�อ EXPORT ติารุ่าง

23

เล1อกติารุ่างที่��ติองการุ่

เล1อกปรุ่ะเภัที่ไฟล�

เล1อกรุ่�ปแบบที่��ติองการุ่

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

24

เล1อกส�งออกซ!�งไมู�ติองก0าหนดค�าอย�างใดครุ่�บ ปกติ�ค�า เล1อกส�งออกซ!�งไมู�ติองก0าหนดค�าอย�างใดครุ่�บ ปกติ�ค�า Default Default จัะก0าหนดจัะก0าหนดใหอย��แลวครุ่�บ แลวอย�าล1มูเล1อก ใหอย��แลวครุ่�บ แลวอย�าล1มูเล1อก Save as file Save as file ดวยน�ะครุ่�บ เพิ่รุ่าะเรุ่าจัะดวยน�ะครุ่�บ เพิ่รุ่าะเรุ่าจัะส�งออกเป*น ส�งออกเป*น ..sql sql

เล1อก เล1อก GoGo

เล1อก เล1อก Save Save ไฟล�ไฟล� mydatabase.sql mydatabase.sql ซ!�งเรุ่าจัะน0าไฟล�น�"ไปใชงานหรุ่1อที่0าการุ่ส0ารุ่องเก'บไวซ!�งเรุ่าจัะน0าไฟล�น�"ไปใชงานหรุ่1อที่0าการุ่ส0ารุ่องเก'บไว

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

การุ่ IMPORT

DATABASE

25

หล�งจัากที่��เรุ่าไดหล�งจัากที่��เรุ่าได .sql .sql มูาแลวข้�"นติ�อไปก'ค1อการุ่ มูาแลวข้�"นติ�อไปก'ค1อการุ่ Import Import เข้าส�� เข้าส�� Database Database ที่��ใหมู�หรุ่1อที่��เด�มูครุ่�บ โดยใน ที่��ใหมู�หรุ่1อที่��เด�มูครุ่�บ โดยใน Database Database ที่��ใหมู�จัะที่��ใหมู�จัะติองไมู�มู� ติองไมู�มู� Table Table ที่��ไดที่0าการุ่ ที่��ไดที่0าการุ่ Export Export ออกติ�"งแติ�แรุ่ก เพิ่รุ่าะเรุ่าออกติ�"งแติ�แรุ่ก เพิ่รุ่าะเรุ่าจัะที่0าการุ่ จัะที่0าการุ่ Import Import เข้าไปใหมู�เข้าไปใหมู�

กรุ่ณ�ที่��ติองการุ่สรุ่าง กรุ่ณ�ที่��ติองการุ่สรุ่าง Database Database ใหมู� หรุ่1อหากมู�อย��แลวก'ข้ามูข้�"นติอนไปไดเลยครุ่�บใหมู� หรุ่1อหากมู�อย��แลวก'ข้ามูข้�"นติอนไปไดเลยครุ่�บ

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

26เล1อกที่��เล1อกที่�� ImportImport

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

27

เล1อกไฟล� เล1อกไฟล� ..sql sql ที่��ไดที่0าการุ่ ที่��ไดที่0าการุ่ Export Export ไวครุ่�บ และเล1อกที่�� ไวครุ่�บ และเล1อกที่�� GoGo

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

28

Database Database ที่��ไดที่0าการุ่ ที่��ไดที่0าการุ่ ImportImport เข้ามูาเข้ามูา

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

MYSQL RELATION CASCADE ON DELETE/ON UPDATE

29

MySQL Relation CasCade On Delete/On Update MySQL Relation CasCade On Delete/On Update ความูสามูารุ่ถข้อง ความูสามูารุ่ถข้อง MySQL MySQL ในการุ่ที่0า ในการุ่ที่0า Relation Relation ก'สามูารุ่ถที่0าไดเช�นเด�ยวก�น โดยก0าหนดชน�ดข้อง ก'สามูารุ่ถที่0าไดเช�นเด�ยวก�น โดยก0าหนดชน�ดข้อง Table Table เป*นแบบ เป*นแบบ InnoDB InnoDB โดยสามูารุ่ถที่0าการุ่ โดยสามูารุ่ถที่0าการุ่ CasCade DELETE CasCade DELETE หรุ่1อ หรุ่1อ UPDATE UPDATE ไดได

Syntax InnoDBSyntax InnoDBCREATE TABLE `customer` (CREATE TABLE `customer` (........) ENGINE=InnoDB; ) 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

ด�ติ�วอย�างด�ติ�วอย�างติ�วอย�างไดที่0าการุ่สรุ่างติ�วอย�างไดที่0าการุ่สรุ่าง Table Table ช1�อ ช1�อ customer customer และ และ audit audit ซ!�งเมู1�อมู�การุ่ลบข้อมู�ลในติารุ่าง ซ!�งเมู1�อมู�การุ่ลบข้อมู�ลในติารุ่าง customer customer ข้อมู�ลที่��อย��ใน ข้อมู�ลที่��อย��ใน audit 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 Relation 2 ติารุ่าง เมู1�อมู�การุ่แกไข้ติารุ่าง เมู1�อมู�การุ่แกไข้หรุ่1อลบข้อมู�ลใน หรุ่1อลบข้อมู�ลใน customer customer ข้อมู�ลใน ข้อมู�ลใน audit audit จัะมู�ผลจัะมู�ผลในการุ่แกไข้ หรุ่1อ ลบเช�นเด�ยวก�นในการุ่แกไข้ หรุ่1อ ลบเช�นเด�ยวก�น

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

SQL SQL ส0าหรุ่�บการุ่สรุ่างส0าหรุ่�บการุ่สรุ่าง TABLE TABLE ช1�อ ช1�อ COUNTRY

CREATE 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 MySQL DSN Data Source ในภัาษา ในภัาษา Programming Programming ส�วนใหญ่�แลวจัะมู� ส�วนใหญ่�แลวจัะมู� Driver Driver ส0าหรุ่�บไวติ�ดติ�อส0าหรุ่�บไวติ�ดติ�อก�บ ก�บ MySQL MySQL อย��แลวครุ่�บ แติ�ในบางภัาษาจัะไมู�สามูารุ่ถเรุ่�ยกใชอย��แลวครุ่�บ แติ�ในบางภัาษาจัะไมู�สามูารุ่ถเรุ่�ยกใชงานไดโดยติรุ่ง และว�ธี�ที่��ใชก�นส�วนมูากก'ค1อการุ่ติ�ดติ�อผ�าน งานไดโดยติรุ่ง และว�ธี�ที่��ใชก�นส�วนมูากก'ค1อการุ่ติ�ดติ�อผ�าน ODBC ODBC ในการุ่เรุ่�ยกใชงาน ในการุ่เรุ่�ยกใชงาน Oracle Oracle ผ�าน ผ�าน ODBC ODBC จัะติองที่0าการุ่ จัะติองที่0าการุ่ Set DSN Name Set DSN Name ก�อนครุ่�บถ!งจัะสามูารุ่ถที่0าการุ่เรุ่�ยก ก�อนครุ่�บถ!งจัะสามูารุ่ถที่0าการุ่เรุ่�ยก Database Database ใชงานไดใชงานได

ดาวโหลด ดาวโหลด MyODBC Driver MyODBC Driver ไดที่�� ไดที่�� www.mysql.com www.mysql.com ในที่��น�"ใชเวอรุ่�ช��น ในที่��น�"ใชเวอรุ่�ช��น 5.1 5.1 เป*นเวอรุ่�ช��นล�าส�ดเป*นเวอรุ่�ช��นล�าส�ดหรุ่1อดาวน�โหลดจัาก หรุ่1อดาวน�โหลดจัาก URL 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

โปรุ่แกรุ่มูโปรุ่แกรุ่มู ODBCODBC

เล1อกที่�� เล1อกที่�� Tab System DSN -> AddTab System DSN -> Add

ว�ธี�การุ่น�"จัะติองมู�การุ่ลง MySQL ODBC 5.1 MySQL ODBC 5.1 เส�ยก�อนเส�ยก�อน

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

36เล1อกที่�� เล1อกที่�� MySQL ODBC 5.1 Driver -> FinishMySQL ODBC 5.1 Driver -> Finish

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

37ก0าหนดช1�อ ก0าหนดช1�อ DSN DSN ในที่��น�"ผมูก0าหนดเป*น ในที่��น�"ผมูก0าหนดเป*น mydatabase mydatabase และเล1อกฐานข้อมู�ล และเล1อกฐานข้อมู�ล mydatabasemydatabase

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

38

ที่ดสอบ ที่ดสอบ Connection Connection คล�กที่�� คล�กที่�� Test Test และโปรุ่แกรุ่มูแสดงสถานะการุ่ติ�ดติ�อและโปรุ่แกรุ่มูแสดงสถานะการุ่ติ�ดติ�อ

DSN DSN ที่��ไดจัากการุ่สรุ่างที่��ไดจัากการุ่สรุ่าง

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

PHP MYSQL CONNECT TO DATABASE

39

PHP MySQL Connect to Database PHP MySQL Connect to Database ส0าหรุ่�บการุ่เข้�ยน ส0าหรุ่�บการุ่เข้�ยน PHP PHP ก�บ ก�บ MySQL MySQL น�"นถ1อเป*นป:จัจั�ยน�"นถ1อเป*นป:จัจั�ยหล�กและไดรุ่�บความูน�ยมูมูากที่��ส�ดในบรุ่รุ่ดา หล�กและไดรุ่�บความูน�ยมูมูากที่��ส�ดในบรุ่รุ่ดา Database Database ที่�"งหลายครุ่�บ เพิ่รุ่าะนอกจัากฟรุ่�ที่�"ง ที่�"งหลายครุ่�บ เพิ่รุ่าะนอกจัากฟรุ่�ที่�"ง 2 2 ติ�วติ�วแลว แลว function function ติ�าง ๆ ติ�าง ๆ PHP PHP ไดออกแบบมูาใหใชก�บ ไดออกแบบมูาใหใชก�บ MySQL Database MySQL Database โดยเฉพิ่าะครุ่�บ โดยเฉพิ่าะครุ่�บ

จั�ดเด�นข้อง จั�ดเด�นข้อง PHP PHP และและ MySQL MySQL ค1อ ค1อ MySQL MySQL สามูารุ่ถที่งานไดอย�างรุ่วดเรุ่'ว และค�อนข้างจัะเก'บข้อมู�ลสามูารุ่ถที่งานไดอย�างรุ่วดเรุ่'ว และค�อนข้างจัะเก'บข้อมู�ลไดหลาย ไดหลาย Record Record

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

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

ถ�เก�ดข้อผ�ดพล�ดต์�มทำ+)ยกต์�วอย"�งให้ให้ทำ��ก�รู้ด�วน&โห้ลดต์�ว extension ช้4)อ php_mysql.dll

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

ไป็ไวในโฟเดอรู้& extension ข้อง php เช้"น C:\AppServ\php5\ext ห้รู้4อส�ม�รู้ถต์รู้วจัสอบ Path ไดทำ+) php.ini

Start -> Run -> php.iniextension_dir = "C:/AppServ\php5\ext".....extension=php_mysql.dll

และก6 Restart Apache ห้รู้4อ Web Server ดวยน"ะครู้�บ เสรู้6จัแลวเป็�นอ�นใช้ได

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 PHP ก�บก�บ MySQLMySQL phpMySQLConnect.phpphpMySQLConnect.php

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

42

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

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

<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 List Table Properties ต์�วอย"�งน+.จัะเป็�นก�รู้เข้+ยนโป็รู้แกรู้ม ต์�วอย"�งน+.จัะเป็�นก�รู้เข้+ยนโป็รู้แกรู้ม PHP PHP ก�บ ก�บ MySQL MySQL เพ4)อแสดงค1ณสมบ�ต์�ข้อง เพ4)อแสดงค1ณสมบ�ต์�ข้อง Table Table ต์�วอย"�งต์�วอย"�ง ListField.phpListField.php

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

44

OutputOutput

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 เพ4)อด2งข้อมลจั�ก Table ม�แสดง ติ�วอย�าง

ListRecord.phpListRecord.php

สามูารุ่เป*นเป*น Query อ1�นๆไดติามูติองการุ่ เช�น

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.phpListRecord.php ต์"อ

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

47

OutputOutput

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

ถาเพิ่��มูเติ�มูการุ่โดยการุ่ที่0า Link เพิ่1�อเรุ่�ยงติามูติ�วอ�กษรุ่ในคอล�มูภั�ติ�างๆ<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.phpListSort.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.phpListSort.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.phpListSort.php ต์"อ

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

51

OutputOutput

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

PHP MYSQL RANDOM RECORD ส��มูรุ่ายการุ่เพิ่1�อแสดง<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.phpRandomRecord.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 RandomRecord.php ติ�อติ�อ

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

54

Output

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

PHP MYSQL LIST RECORD PAGINGPHP 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 เพิ่1�อด!งข้อมู�ลจัาก 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 59: PHP-5 ติดต่อฐานข้อมูล  MySQL

59

Output

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 เพ4)อคนห้�ข้อมลจั�ก 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 63: PHP-5 ติดต่อฐานข้อมูล  MySQL

63

Output

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 เพ4)อคนห้�ข้อมลจั�ก 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 69: PHP-5 ติดต่อฐานข้อมูล  MySQL

69

Output

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 เพ4)อเพ�)มข้อมลลงใน 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 72: PHP-5 ติดต่อฐานข้อมูล  MySQL

72

Output

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 74: PHP-5 ติดต่อฐานข้อมูล  MySQL

74

Output

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

PHP MYSQL CHECK ALREADY EXIST ADD/INSERT

RECORD การุ่ติรุ่วจัข้อมู�ลซ0"าก�อนเพิ่��มู

<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 77: PHP-5 ติดต่อฐานข้อมูล  MySQL

77

Output

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 CheckExistRecordAddSave.php ติ�อติ�อ

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

80

Output

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

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

PHP MySQL Edit/Update Record ต์�วอย"�งน+.จัะเป็�นก�รู้เข้+ยนโป็รู้แกรู้ม PHP ก�บ MySQL เพ4)อแกไข้ข้อมลลงใน Table

81

EditRecordList.php

EditRecordForm.php

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

xxxxxxxxx

Form แกไข้

xxx xxx แกไข้

xxx xxx แกไข้

ด2งข้อมลทำ+)เล4อกม�แกไข้

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 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 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 88: PHP-5 ติดต่อฐานข้อมูล  MySQL

OUTPUT

88

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 เพ4)อลบข้อมลใน 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 ในก�รู้ต์รู้วจัสอบความูถ�กติองก"อนก�รู้ทำ��ก�รู้บ�นที่!กข้อมู�ลจัรู้�ง ห้ล�กก�รู้ค4อ เม4)อม+ก�รู้ Begin แลว ภ�ยใต์ค��ส�)งทำ+)อย"ในเง4)อนไนน+.จัะม+ก�รู้ต์รู้วจัสอบว"�ม+ก�รู้ทำ��ง�นถกต์อง ห้รู้4อม+ข้อผ�ดพล�ดห้รู้4อไม" ถ�ไม"ม+ข้อผ�ดพล�ดก6จัะทำ��ก�รู้ Commit ห้รู้4อถ�ม+ข้อผ�ดพล�ดโป็รู้แกรู้มจัะทำ��ก�รู้ Rollback ค4อยกเล�กข้อมลทำ+)ไดทำ��ก�รู้กรู้ะทำ��ในแต์"แรู้ก

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

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

skip-innodb

แกเป็�น #skip-innodb

### Restart MySQL Service ###95

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

ติ�วอย�างต์�วอย"�งน+.จัะเป็�นก�รู้เพ�)มข้อมล ไดสมม1ต์�ก�รู้เพ�)มข้อมลซ�.� ซ2)งม+ Primary Key ช้4)อ CustomerID ซ2)งจัะส�ม�รู้ถเพ�)มข้อมลส��เรู้6จัใน Statement แรู้ก และ Statement ทำ+) 2 จัะไม"ส�ม�รู้ถเพ�)มข้อมลได และเม4)อม+ก�รู้ RollBack ข้อมล Statement แรู้กทำ+)ถก Insert ไป็แลวก6จัะถกยกเล�กในทำ�นทำ+ ส��ห้รู้�บก�รู้ใช้ 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 98: PHP-5 ติดต่อฐานข้อมูล  MySQL

OUTPUT

98

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') ]