PHP-5 ติดต่อฐานข้อมูล MySQL
Embed Size (px)
description
Transcript of PHP-5 ติดต่อฐานข้อมูล MySQL
PHP MySQL
PHP-5 MySQL 11MySQL Introduction
MySQL () SQL. MySQL MySQL AB MySQL
MySQL 2 David Axmark, Allan Larsson Michael "Monty" Widenius.
(Sun Microsystems, Inc.) MySQL AB MySQL AB
22
SAP (MAX DB) , ()
* MySQL Enterprise* MySQL Cluster* MySQL Embedded* MySQL Community (opensource )
33
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)
44 (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
55
MySQL
o BIT ( MyISAM, InnoDB, Memory)o TINYINTo SMALLINTo MEDIUMINTo INTo BIGINT
66* o DATETIMEo DATEo TIMESTAMPo TIMEo YEAR
o CHARo VARCHARo BINARYo VARBINARYo BLOBo TEXTo ENUMo SET
77
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.
88 (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
99MySQL 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.com1010phpMyAdmin phpMyAdmin???
phpMyAdmin ?phpMyAdmin PHP MySQL Web Browser MySQL Database Create Database/Table/Field //
phpMyAdmin ? Appserv phpMyAdmin http://localhost/phpMyAdmin
phpMyAdmin Package Appserv
Appserv & phpMyAdmin http://www.thaicreate.com/php/php-install-apache.html
phpMyAdmin Download phpMyAdmin http://www.phpmyadmin.net/ 1111
12 phpMyAdmin Web Browser 12
13 Database Table 13
14 (Field,Record Column) 14 MydatabaseFieldsTypeDescriptionCustomerIDvarchar(4)(PK)Namevarchar(50)Emailvarchar(50)CountryCodevarchar(2)(FK)Budgetdouble Useddouble 15FieldsTypeDescriptionCountryCodeVARCHAR( 2 )(PK)CountryNameVARCHAR(50)FieldsTypeDescriptionAuditIDint(4) Audit(PK)CustomerIDvarchar(4)(FK)Date date UseddoubleCustomerAuditCountry15MySQL phpMyAdmin Create Database & Table Database Table phpMyAdmin phpMyAdmin http://localhost/phpMyAdmin 16 User/Password phpMyAdmin Default root/root
16 phpMyAdmin Web Browser Create Database
17
Database Name Create
Create Table
Table Go 17
18 18 Table Table 3 Table customer,country,audit Table Customer
1919
20Table country Table audit
20 Table
Rows Customer 21
Rows country
Rows audit Rows Customer21 Export Database 22 Export MySQL Backup
22 Export 23
2324 Default Save as file .sql
Go
Save mydatabase.sql 24 Import Database 25 .sql Import Database Database Table Export Import
Database 25
26 Import 2627
.sql Export Go 27
28Database Import 28MySQL Relation CasCade On Delete/On Update 29MySQL Relation CasCade On Delete/On Update MySQL Relation Table InnoDB CasCade DELETE UPDATE
Syntax InnoDBCREATE TABLE `customer` (....) ENGINE=InnoDB;
29Table : 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 30Table : 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
3132
Relation 2 customer audit 32SQL 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');
3333MySQL Data Source Config (DSN) 34MySQL 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 Tools3435
ODBC
Tab System DSN -> Add MySQL ODBC 5.1 3536
MySQL ODBC 5.1 Driver -> Finish 3637
DSN mydatabase mydatabase 3738
Connection Test
DSN 38PHP MySQL Connect to Database 39PHP 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 739 extension php_mysql.dll
php_mysql.dll http://www.thaicreate.com/php/php_mysql.dll.html extension php C:\AppServ\php5\ext Path php.ini
Start -> 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 4040
iamsanya.com
41 PHP MySQL phpMySQLConnect.php4142
42PHP MySQL List Table Properties ()
iamsanya.com
43PHP MySQL List Table Properties PHP MySQL Table
ListField.php4344
Output 44PHP MySQL List Record ()
iamsanya.com
CustomerID Name Email CountryCode Budget Used
46ListRecord.php 4647
Output 47 Link