206
2002/9/15 國立中央大學電算中心 陳慶彥 1 PHP+MySQL動態網頁 (For Linux) 國立中央大學電算中心 陳慶彥 [email protected] http://www.ncu.edu.tw/~center22

PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 1

PHP+MySQL動態網頁設計

(For Linux)

國立中央大學電算中心陳慶彥

[email protected]://www.ncu.edu.tw/~center22

Page 2: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 2

主題

系統環境簡介

MySQL 管理設定PHP程式設計實作範例

Page 3: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 3

系統環境簡介

PHP是一種伺服端(server-side),跨平台(cross-platform),簡單易學的HTML嵌入式非編譯性語言(HTML embedded scripting language)。它通常以模組(module)的形式和Apache伺服器結合,提供多種連結資料庫的介面,如MySQL,mSQL,PostgreSQL,Sybase ,Informix,InterBase等。

PHP是屬於公開式程式(OPEN SOURCE),歡迎用於商業或非商業性質用途上,而且是完全免費的,這是一種屬於大家的程式語言。

PHP的表現並不遜色於其他的同類伺服端介面語言( iHTML, Cold Fusion, locomotive, JSP,ASP,...),執行效率和開發速率也比 Perl、C/C++ CGI等快很多

Page 4: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 4

系統環境簡介

Apache Server

前端瀏覽器

Internet MySQL

PHP

Page 5: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 5

系統環境簡介-PHP比ASP優秀的七個理由

原作者:John來 源: php.weblogs.com

ASP是微軟公司實現動態網頁的一種技術。ASP支持一些腳本語言,主要以VBScript為主。與ASP相比較,你還可以選擇另一中開放源代碼編程語言──PHP,PHP可以運行在多種操作系統下,其中包括Linux和windows。

雖然ASP是一種不錯的技術,但從長遠考慮我相信PHP在將來的技術領域里會有不凡的表現。

Page 6: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 6

系統環境簡介-PHP比ASP優秀的七個理由

1、速度、速度、速度

ASP是永遠也不會象PHP這樣快的,因為ASP是建立在COM體系結構之上的。當用VBScript寫ASP腳本時,實際上實在使用COM的對象,當向用戶瀏覽器發送信息時,它用的是Response對象的write方法,當它訪問資料庫和文件系統的時候,它用的是其他的COM對象。這些COM對象的使用使運行速度下降。

在PHP代碼中,所有的工作都運行在PHP的內存空間中,也就是說PHP不是基於COM對象的,所以的他的運行速度會快一些。

Page 7: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 7

系統環境簡介-PHP比ASP優秀的七個理由

下面是我們在MSSQL7 40次運行一個查詢語句的執行時間統計:

Querying MSSQL7 時間(秒)PHP用MSSQL extension 01.88PHP用ODBC extension 09.54ASP用ODBC via COM(ADO) 17.28ASP用OLEDB via COM 06.19

當我們使用PHP.ODBC,訪問資料庫用了9.54秒,而ASP用COM接口去連接資料庫需要比PHP所用的時間高80%。

Page 8: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 8

系統環境簡介-PHP比ASP優秀的七個理由

OLEDB是微軟的一種高速訪問資料庫的技術,他比ODBC要快。但當我們所PHP和OLEDB的效率實驗時,結果是PHP的整體性能比OLEDB高200%,如此的出一個結論,如果不採用COM可以獲得較快的執行速度。

當然PHP的執行速度還不是最快的,但我們才剛剛開始起步,我們會逐步的完善他,而且PHP是一種開放源代碼編程語言,世界上許多程序員在不斷的完善他的技術,相信他一定會比非開放源代碼系統獲得更高的執行速度的。

Page 9: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 9

系統環境簡介-PHP比ASP優秀的七個理由

2、高級內存管理在IIS4下,一個ASP腳本header.asp,如果被20個

頁面所包含,那么運行的時候,在內存當中會保留這20個header.asp的編譯副本,IIS5解決了這個問題,但只有windows2000才支持IIS5,由于這種IIS5的不能向下兼容的原因,許多服務器仍然要使用IIS4下的低級的內存管理。

而在PHP中,不會存在這種問題,只有當require時,才會調用某個include文件。

Page 10: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 10

系統環境簡介-PHP比ASP優秀的七個理由

3、沒有後顧之憂我非常憎恨的一件事情就是會買到一件不放心的產

品,比如當你買了一輛汽車,但由於發動機是壞的,你需要更換他﹔當你買了一棟房子的時候,你要去修補漏雨的屋頂。

那麼ASP就像這些產品一樣,當你買了ASP之後,如果你需要加密技術,你就要買ASPEncrypt﹔當你需要Email管理時,你就要買ServerObjets Qmail﹔當你需要文件上傳時,你還要買Artisans SA-FileUp.

Page 11: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 11

系統環境簡介-PHP比ASP優秀的七個理由

4、MySQL使PHP更精彩PHP與MySQL的組合既簡單又精彩。PHP有許多管理和

維護MySQL的工具,對MySQL的支持是最全面的。許多有用的函數如mysql_insert_id和mysql_affected_rows等,其他的資料庫則沒有。

ASP和PHP都是中型網站的較理想的解決方案,但PHP與MySQL的緊密結合使PHP更加優越。

MySQL的虛度比Microsoft Access速度快,Mssql和Oracle速度比MySQL要快,可是費用較高。

Page 12: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 12

系統環境簡介-PHP比ASP優秀的七個理由

5、Java和C++的變成風格這個優點只是一個看法。我知道你不會相信一個Visual Basic程式設計者會因

為PHP有Java和C++的編程風格而轉向使用PHP,但你會相信有許多人還是喜歡Java和C++的,PHP是一種具有這兩種語言的編程風格的較容易學習的語言。

PHP支持結構化編程,在VB因為他的繼承性使用類別還是較少的。而PHP的象Java和C++一樣的繼承性使一個大型的程序中到處都充滿了類別的影子。

Page 13: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 13

系統環境簡介-PHP比ASP優秀的七個理由

6、Bug的處理。你是否曾經要求Microsoft修改ASP的一些Bug呢?如

果你不是一個象Boeing一樣的組織,你無法獲得迅速而恰當的修改,那麼當你遇到Bug而停止工作時,如果這個Bug不修改,你將無法繼續你的工作的。

PHP,當你發現Bug時,你可以修改他,如果你沒有專家的水平,你可以請專家修改,並且你的修改會得到開放代碼組織的接受和認可。

Page 14: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 14

系統環境簡介-PHP比ASP優秀的七個理由

7、移植性Microsoft經常修改ASP,下一版本叫ASP+,現在已經開始

測試。我相信他們會修改許多我曾經提到過的問題。但也無庸質疑的,Microsoft會將你限制在他們的產品範圍之內的。

我曾經是一個apple的程式設計者,當windows95問世後,我開始轉變,但許多原來的程式碼都無法在win95下運行,這些程式碼現在被放在一邊,我非常的遺憾。所以我不希望在windows下再出現這樣的悲劇。

不要忘記Novell曾經也像Microsoft的網路操作系統一樣熱及一時,但現在已經沒有許多人用了,那麼Microsoft也會這樣的。我找到了PHP,我仍舊在windows下工作,但我知道PHP代碼可以運行在Solaris、Linux和許多其他的操作系統中,我的心裡感到安慰。

Page 15: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 15

系統環境簡介-Linux OSftp://ftp.linux.org.tw/pub/CLE/

Page 16: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 16

MySQL管理設定

Page 17: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 17

MySQL管理設定- What is MySQL?

What is MySQL?MySQL is a true multi-user, multi-threaded

SQL database server. SQL (Structured Query

Language) is the most popular and standardized database language in the world. MySQL is

a client/server implementation that consists of a server daemon mysqld and many dierent

Page 18: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 18

MySQL管理設定- featuresThe main features of MySQLFully multi-threaded using kernel threads. That means it easily can use multiple CPUsif available.C, C++, Eiel, Java, Perl, PHP, Python and TCL APIs. Works on many dierent platforms. Many column types: signed/unsigned integers 1, 2, 3, 4 and 8

bytes long, FLOAT,DOUBLE, CHAR, VARCHAR, TEXT, BLOB, DATE, TIME, DATETIME, TIMESTAMP, YEAR, SET and ENUM types.

Page 19: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 19

MySQL管理設定- featuresThe main features of MySQL(cont)Very fast joins using an optimized one-sweep multi-join.Full operator and function support in the SELECT

and WHERE parts of queries. Example:mysql> SELECT CONCAT(first_name, " ", last_name) FROM

tbl_name WHERE income/dependents > 10000 AND age > 30;

SQL functions are implemented through a highly-optimized class library and should be as fast as they can get! Usually there shouldn't be any memory allocation at all after query initialization.

Page 20: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 20

MySQL管理設定- featuresThe main features of MySQL(cont)Very ODBC (Open-DataBase-Connectivity) for Windows95 (with source). All ODBC 2.5 functions and many others. You can, for example, use Access to connect to your MySQL server.Handles large databases. We are using MySQL

with some databases that contain 50,000,000 records.Year 2000 compliance

Page 21: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 21

MySQL 管理設定- Installing MySQL

How to get MySQL?http://www.mysql.com/Operating systems supported by MySQL

AIX 4.x , BSDI 2.x , BSDI 3.0, 3.1 and 4.x , DEC UNIX 4.x ,FreeBSD 2.x , FreeBSD 3.x , HP-UX 10.20 , HP-UX 11.x ,Linux 2.0+ , MacOS X Server, NetBSD 1.3/1.4 , NetBSD1.3 Alpha (Requires GNU make), OpenBSD > 2.5 , OpenBSD < 2.5 , OS/2 Warp 3 , OS/2 Warp 4, SGI Irix6.x , Solaris 2.5, 2.6 and 2.7 , SunOS 4.x , SCO OpenServer , SCO UnixWare 7.0.1, Win95, Win98 and NT

Page 22: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 22

MySQL 管理設定- Installing MySQL

Installing and Configuring MySQL for UNIXTo install MySQL on the UNIX platform, go to the Downloads area of the MySQL Web site and download the binary distribution specific to your platform. Next, place the zipped tar file in a directory such as /usr/local/.Unzip and unpack this file by typing

gunzip [filename].tar.gztar -xvf [filename].tar

These commands will create a /usr/local/mysql_[version]/ directory

structure containing the MySQL binaries.

Page 23: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 23

MySQL 管理設定- Installing MySQL

Installing and Configuring MySQL for UNIXTo run MySQL, do the following:1.Navigate to the MySQL scripts directory by typing cd/usr/local/mysql_[version]/scripts/

2.Type mysql_install_db to create an initial database.3.Type cd /usr/local/mysql_[version]/bin/4.Type safe_mysqld & to start the MySQL server process.5.Type mysqladmin version to see that MySQL server is indeed running.

Page 24: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 24

MySQL 管理設定- Installing by RPM

Page 25: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 25

MySQL 管理設定- Installing by RPM

Page 26: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 26

MySQL 管理設定- Installing by RPM

Page 27: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 27

MySQL 管理設定- Installing by RPM

Page 28: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 28

MySQL 管理設定- Installing MySQL

Starting and stopping MySQLautomatically

shell> mysql.server start

shell> mysql.server stop

Page 29: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 29

MySQL 管理設定- Installing MySQL

Page 30: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

國立中央大學電算中心 陳慶彥 30

MySQL 管理設定- Perl+MySQLInstall DBI-1.13

[root@fax /tmp]# zcat dbi-1.13.tar.gz | tar xvf –

[root@fax /tmp]# cd DBI-1.13

[root@fax DBI-1.13] perl Makefile.PL

[root@fax DBI-1.13] make

[root@fax DBI-1.13] make test

[root@fax DBI-1.13] make install

Page 31: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

國立中央大學電算中心 陳慶彥 31

MySQL 管理設定- Perl+MySQLInstall msql-mysql-modules-1.2210

[root@fax /tmp]# zcat msql-mysql-modules-1.2210.tar.gz | tar xvf –

[root@fax /tmp]# cd Msql-Mysql-modules-1.2210

[root@fax Msql-Mysql-modules-1.2210] perl Makefile.PL

[root@fax Msql-Mysql-modules-1.2210]make

[root@fax Msql-Mysql-modules-1.2210]make test

[root@fax Msql-Mysql-modules-1.2210]make install

Page 32: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 32

MySQL 管理設定- MySQL操作範例

Connect to the MySQL Server

shell> mysql [-h host_name] [-u user_name] [-p your_password]

Shell> mysql –h localhost –u joe

Shell> mysql –h localhost

Shell> mysql –u joe

Shell> mysql

Page 33: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 33

MySQL 管理設定- MySQL操作範例

[s8940@ccy public_html]$ mysql -u s8940 test

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 742 to server version: 3.22.30

Type 'help' for help.

mysql>

Page 34: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 34

MySQL 管理設定- MySQL操作範例

mysql> help;MySQL commands:

help (\h) Display this text

? (\h) Synonym for `help'

clear (\c) Clear command

connect (\r) Reconnect to the server. Optional arguments are db and host

exit (\q) Exit mysql. Same as quit

go (\g) Send command to mysql serverquit (\q) Quit mysql

status (\s) Get status information from the server

use (\u) Use another database. Takes database name as argument

Connection id: 742 (Can be used with mysqladmin kill)

Page 35: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 35

MySQL 管理設定- MySQL操作範例

mysql> show tables;+----------------+

| Tables in test |

+----------------+

| s8940 |

| testac |

+----------------+

2 rows in set (0.00 sec)

Page 36: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 36

MySQL 管理設定- MySQL操作範例

mysql> CREATE TABLE s8941

-> (CreateDate DATETIME ,

-> StudentName char(50) ,

-> StudentID char(10) ,

-> Address char(100) ,

-> Birthday DATE );

Query OK, 0 rows affected (0.01 sec)

mysql> show tables;

+----------------+

| Tables in test |

+----------------+

| s8940 |

| s8941 |

| testac |

+----------------+

3 rows in set (0.01 sec)

Page 37: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 37

MySQL 管理設定- MySQL操作範例

mysql> insert into s8941 values('2000/07/18 15:12:01','陳慶彥','87001',

-> '中壢市五權里上山座屋38號','1988/01/01');Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋38號 | 1988-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

1 row in set (0.01 sec)

Page 38: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 38

MySQL 管理設定- MySQL操作範例

mysql> insert into s8941 values('2000/07/18 15:19:01','陳二','87002',

-> '中壢市五權里上山座屋38號','1977/01/01');Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋38號 | 1988-01-01 |

| 2000-07-18 15:19:01 | 陳二 | 87002 | 中壢市五權里上山座屋38號 | 1977-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

Page 39: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 39

MySQL 管理設定- MySQL操作範例

mysql> update s8941 set Address='金門縣金沙鎮光前村陽翟1號' where StudentID='87002';

Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋38號 | 1988-01-01 |

| 2000-07-18 15:19:01 | 陳二 | 87002 |金門縣金沙鎮光前村陽翟1號| 1977-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

Page 40: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 40

MySQL 管理設定- MySQL操作範例

mysql> delete from s8941 where StudentID='87002';Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋38號 | 1988-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

mysql> quit

Page 41: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 41

MySQL 管理設定- MySQL操作範例

Perl連結MySQL執行資料的存取

use DBI;use strict;$dbh = DBI->connect(“DBI:mysql:資料庫”,“帳號”,”密碼“);$sth = $dbh->prpare($statement);$sth->execute;$sth->finish;$dbh->disconnect;

Page 42: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 42

MySQL 管理設定- MySQL操作範例

實作1 – create_table.cgi#!/usr/bin/perl

use DBI;

use strict;

my $dbh;

if ($dbh=DBI->connect("DBI:mysql:test","s8940","") ){

print "連結資料庫:test OK!\n";

}else { print "無法連結資料庫:test!"; exit(0); }

Page 43: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 43

MySQL 管理設定- MySQL操作範例

實作1 – create_table.cgi (cont)my $sth=$dbh->prepare(" DROP TABLE s8940 ");

if ($sth->execute ){

$sth->finish;

print "表格:s8940存在資料庫:test之中,刪除表格:s8940 成功\!\n";

}else {

print "表格:s8940不存在資料庫:test之中,刪除表格:s8940 失敗\!\n"; }

Page 44: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 44

MySQL 管理設定- MySQL操作範例

實作 1– create_table.cgi (cont)my $sth=$dbh->prepare("CREATE TABLE s8940

( CreateDate DATETIME ,

StudentName char(50) ,

StudentID char(10) ,

Address char(100) ,

Birthday DATE)

");

Page 45: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 45

MySQL 管理設定- MySQL操作範例

實作1 – create_table.cgi (cont)if ($sth->execute){

print "表格:s8940在資料庫:test上create OK!\n";

}

else{

print "表格:s8940在資料庫:test上create 失敗!: $dbh->errstr\n";

}

$sth->finish;

Page 46: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 46

MySQL 管理設定- MySQL操作範例

實作1 – create_table.cgi (cont)my $sth=$dbh->prepare("INSERT INTO s8940 VALUES

(‘2000/07/18 15:12:01’,‘陳慶彥’,‘87001’,‘中壢市五權里上山

座屋38號','1988/01/01') ");

if ($sth->execute){

print "表格:s8940新增資料 OK!\n";

}else{ print "表格:s8940新增資料失敗!: $dbh->errstr\n"; }

$sth->finish; $dbh->disconnect; exit(0);

Page 47: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 47

MySQL 管理設定- MySQL操作範例

實作2 – select_table.cgi#!/usr/bin/perl

use DBI;

use strict;

my $dbh;

if ($dbh=DBI->connect("DBI:mysql:test","s8940","") ){

print "連結資料庫:test OK!\n"; }

else { print "無法連結資料庫:test!"; exit(0); }

Page 48: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 48

MySQL 管理設定- MySQL操作範例實作2 – select_table.cgi (cont)

my $sth = $dbh->prepare("SELECT * FROM s8940");

if ($sth->execute ){

my $table = $sth->fetchall_arrayref;

my($i, $j);

for $i ( 0 .. $#{$table} ) {

for $j ( 0 .. $#{$table->[$i]} ) { print "$table->[$i][$j]\t"; }

print "\n";

}

Page 49: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 49

MySQL 管理設定- MySQL操作範例

實作2 – select_table.cgi (cont)}

else {

print "表格:s8940查詢資料失敗\!\n";

}

$sth->finish;

$dbh->disconnect;

exit;

Page 50: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 50

MySQL 管理設定- privilege system

如何連結到資料庫

shell> mysql -u root mysqlmysql> UPDATE user SET Passwordmysql> = PASSWORD('new_password')mysql> WHERE user='root';mysql> FLUSH PRIVILEGES;mysql> quit;

Page 51: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 51

MySQL 管理設定- privilege system

Page 52: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 52

MySQL 管理設定- privilege system資料表格: host

欄位 類型 Null default 備註

Host char(60) 否 來存取的主機名稱

Db char(32) 否 資料庫名稱

Select_priv enum('N','Y') 否 N 對Table做selectInsert_priv enum('N','Y') 否 N 對Table做InsertUpdate_priv enum('N','Y') 否 N 對Table做UpdateDelete_priv enum('N','Y') 否 N 對Table做DeleteCreate_priv enum('N','Y') 否 N Create Database,TablesDrop_priv enum('N','Y') 否 N Drop Database,Tables

Grant_priv enum('N','Y') 否 N Grant Database,Tables,允許把自己的權限開放給其他使用者

References_priv enum('N','Y') 否 NIndex_priv enum('N','Y') 否 N Create/Drop IndexAlter_priv enum('N','Y') 否 N Alter tables

Page 53: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 53

MySQL 管理設定- privilege system資料表格: db

欄位 類型 Null 內定值 額外

Host char(60) 否 來存取的主機名稱Db char(32) 否 資料庫名稱User char(16) 否 使用者

Select_priv enum('N','Y') 否 N 對Table做selectInsert_priv enum('N','Y') 否 N 對Table做InsertUpdate_priv enum('N','Y') 否 N 對Table做UpdateDelete_priv enum('N','Y') 否 N 對Table做DeleteCreate_priv enum('N','Y') 否 N Create Database,TablesDrop_priv enum('N','Y') 否 N Drop Database,Tables

Grant_priv enum('N','Y') 否 N Grant Database,Tables,允許把自己的權限開放給其他使用者

References_priv enum('N','Y') 否 NIndex_priv enum('N','Y') 否 N Create/Drop IndexAlter_priv enum('N','Y') 否 N 修正資料表

Page 54: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 54

MySQL 管理設定- privilege system資料表格: user

Host User Password Select_priv

Insert_priv

Update_priv

Delete_priv ...

localhost root 70a1cfcf1ddaba6c Y Y Y Y ...

140.115.11.235 root 70a1cfcf1ddaba6c Y Y Y Y ...

localhost N N N N ...

140.115.11.% ccy 70a1cfcf1ddaba6c Y Y Y Y ...

localhost neotech 5a5394000c186982 Y Y Y Y ...

Page 55: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 55

MySQL 管理設定- privilege system

關於Password, MySQL採用自己的加密方式,所有存在user中的password都必須經過password()函數的處理.shell> mysql -u root mysqlmysql> UPDATE user SET

Password=PASSWORD('new_password')WHERE user='root';

mysql> FLUSH PRIVILEGES;

Page 56: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 56

MySQL 管理設定- privilege system

權限運作模式.stage 1: Connection verication

1. The host from which you connect2. Your MySQL user name

stage 2: Request verication

Page 57: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 57

MySQL 管理設定- privilege system

stage 1: Connection vericationMySQL對user資料表的排序為Host User,排序時描述愈清楚的排在愈前面,含萬用字元在後面,Suppose the user table looks like this:+--------------+----------+---------------+-| Host | User | password |..+--------------+----------+---------------+-| % | root | ... |..| % | jeffrey | password1 | .. | localhost | root | ... |..| localhost | | ... |..|140.115.1.1 | | password2 | .. +--------------+----------+---------------+-從140.115.1.1以jeffrey要求連線時採用的密碼為何?

Page 58: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 58

MySQL 管理設定- privilege system

stage 1: Connection verication(cont)答案: password2+--------------+----------+---------------+-| Host | User | password |..+--------------+----------+---------------+-|140.115.1.1 | | password2 | ..| localhost | root | ... |..| localhost | | ... |..| % | jeffrey | password1 | .. | % | root | ... |..+--------------+----------+---------------+-

Page 59: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 59

MySQL 管理設定- privilege system

Page 60: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 60

MySQL 管理設定- privilege system

stage 2: Request verication1. 如果是Server管理相關要求,直接尋找第一階段user table符合的記錄中,相對的權限如reload_priv, shutdown_priv等

2. 如果是table或是database相關的動作如select,那就轉移下面的邏輯作判斷global privileges #第一階段user table符合的記錄中其他權限OR (database privileges AND host privileges)OR table privileges OR column privileges

Page 61: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 61

MyODBC for Windows

DownLoad

Page 62: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 62

MyODBC - 將檔案解壓縮

Page 63: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 63

MyODBC – 執行 setup.exe

執行Setup.exe

Page 64: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 64

MyODBC – 設定Data Sources

Page 65: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 65

MyODBC – 設定Data Sources

Page 66: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 66

MyODBC – 安裝完成

Page 67: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 67

MyODBC – 日後的修改設定

1

23

Page 68: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 68

MyODBC – 將資料表匯入Access

開啟Access 2000

Page 69: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 69

MyODBC – 執行匯入功能

Page 70: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 70

MyODBC – 選擇檔案類型-ODBC資料庫()

Page 71: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 71

MyODBC – 選擇Data Sources

Page 72: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 72

MyODBC – 設定Database等相關參數

Page 73: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 73

MyODBC – 選擇匯入的資料表

Page 74: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 74

MyODBC –匯入完成

Page 75: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 75

MyODBC –匯入資料表之範例

Page 76: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 76

MyODBC –連結MySQL資料庫

連結資料表

Page 77: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 77

MyODBC –連結MySQL資料庫

1. ODBC資料庫

2. 選MySQL

Page 78: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 78

MyODBC –連結MySQL資料庫

2

1

Page 79: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 79

MyODBC –連結MySQL資料庫

21

Page 80: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 80

MyODBC –連結MySQL資料庫範例

Page 81: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 81

PHP程式設計

Page 82: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 82

PHP程式設計-IntroductionWhat is PHP ?

PHP is a server-side HTML-embedded scripting language

Example . An introductory example<html>

<head><title>Example</title>

</head><body>

<?php echo "Hi, I'm a PHP script!"; ?></body>

</html>

Page 83: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 83

PHP程式設計-IntroductionWhat can PHP do ?

Writing a database-enabled web page is incredibly. The following databases are currently supported:Adabas D InterBase Solid

dBase mSQL Sybase

Empress MySQL Velocis

FilePro Oracle Unix dbm

Informix PostgreSQL

Page 84: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 84

PHP程式設計-Introduction

What can PHP do ? (cont)PHP also has support for talking to other services using protocols such as IMAP, SNMP, NNTP, POP3, or even HTTP. You can also open raw network sockets and interact using other protocols.

Page 85: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 85

PHP程式設計-featurePHP featuresHTTP authentication with PHPGIF creation with PHPFile upload supportHTTP cookie supportdatabase supportRegular expressions

Page 86: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 86

PHP程式設計-InstallationInstall From Source on UNIXDownloading Sourcehttp://www.php.net

Installation on UNIX systemsThis section will guide you through the configuration and

installation of PHP. Prerequisite knowledge and software: • • An ANSI C compiler

• A web server

Page 87: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 87

PHP程式設計-InstallationQuick Installation Instructions

1. gunzip apache_1.3.x.tar.gz2. tar xvf apache_1.3.x.tar3. gunzip php-3.0.x.tar.gz4. tar xvf php-3.0.x.tar5. cd apache_1.3.x6. ./configure --prefix=/www7. cd ../php-3.0.x8. ./configure --with-mysql --with-apache=../apache_1.3.x --enable-track-vars9. make10. make install11. cd ../apache_1.3.x

Page 88: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 88

PHP程式設計-InstallationQuick Installation Instructions(cont)

12. ./configure --prefix=/www --activate-module=src/modules/ php3/libphp3.a

13. make14. make install15. cd ../php-3.0.x16. cp php3.ini-dist /usr/local/lib/php3.ini17. Edit your httpd.conf or srm.conf file and add:

AddType application/x-httpd-php3 .php3

18. Use your normal procedure for starting the Apache server. (You must stop and restart the server, not just cause the server to reload by use a HUP or USR1 signal.)

Page 89: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 89

PHP程式設計-InstallationConfiguration

There are two ways of configuring PHP. •• Using the "setup" script that comes with PHP. This script asks you a series of questions (almost like the "install" script of PHP/FI 2.0) and runs "configure" in the end. To run this script,type ./setup. • This script will also create a file called "do-conf", this file will contain the options passed to configure. You can edit this file to change just a few options without having to re-run setup. Then type ./do-conf to run configure with the new options.

Page 90: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 90

PHP程式設計-Configuration

The configuration fileThe configuration file (called php3.ini in

PHP 3.0, and simply php.ini as of PHP 4.0) is read when PHP starts up.

Page 91: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 91

PHP程式設計-Install by RPM

Page 92: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 92

PHP程式設計-Install by RPM

Page 93: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 93

PHP程式設計-PHP4 For Windows 2000

下載軟體

Page 94: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 94

PHP程式設計-PHP4 For Windows 2000將檔案解壓縮到 c:\php4 的目錄下

Page 95: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 95

PHP程式設計-PHP4 For Windows 2000將檔案c:\php4\php.ini.inst另存新檔到 c:\winnt\php.ini

Page 96: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 96

PHP程式設計-PHP4 For Windows 2000將檔案php4isapi.dll與php4ts.dll複製到 c:\winnt\system32

Page 97: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 97

PHP程式設計-PHP4 For Windows 2000開啟主控台,點選Internet Information Services之預設的Web站台,點選滑鼠右鍵後,開啟內容

Page 98: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 98

PHP程式設計-PHP4 For Windows 2000

1

2

Page 99: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 99

PHP程式設計-PHP4 For Windows 2000

12

Page 100: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 100

PHP程式設計-PHP4 For Windows 2000

2

13

4

5

6

Page 101: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 101

PHP程式設計-PHP4 For Windows 2000

Stop IIS5

Start IIS5

Page 102: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 102

PHP程式設計-PHP4 For Windows 2000新增一個網站的虛擬目錄

Page 103: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 103

PHP程式設計-PHP4 For Windows 2000設定虛擬目錄名稱

Page 104: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 104

PHP程式設計-PHP4 For Windows 2000設定虛擬目錄對應的實體路徑

Page 105: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 105

PHP程式設計-PHP4 For Windows 2000設定執行功能後結束

Page 106: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 106

PHP程式設計-PHP4 For Windows 2000設定該虛擬目錄的起始網頁

1 2

Page 107: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 107

PHP程式設計-PHP4 For Windows 2000設定該虛擬目錄的起始網頁

Page 108: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 108

PHP程式設計-PHP4 For Windows 2000撰寫測試網頁phpinfo.php

Page 109: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 109

PHP程式設計-PHP4 For Windows 2000測試 phpinfo.php

Page 110: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 110

PHP程式設計-Basic syntax

Escaping from HTMLThere are four ways of escaping from HTML and entering "PHP code mode“

Example. Ways of escaping from HTML1. <? echo ("this is the simplest, an SGML processing instruction\n"); ?>

2. <?php echo("if you want to serve XML documents, do like this\n"); ?>

Page 111: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 111

PHP程式設計-Basic syntax

Escaping from HTML (cont)3. <script language="php">

echo ("some editors (like FrontPage) don'tlike processing instructions");

</script>4. <% echo ("You may optionally use ASP-style tags"); %><%= $variable; # This is a shortcut for "<%echo .." %>

Page 112: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 112

PHP程式設計-Basic syntax

Instruction separation

<?phpecho "This is a test";

?>

<?php echo "This is a test" ?>

Page 113: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 113

PHP程式設計-Basic syntax

Comments

<?phpecho "This is a test"; //This is c++ style/* This is a multi line comment

yet another line of comment */echo "This is yet another test";echo "One Final Test"; # This is shell-style ?>

Page 114: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 114

PHP程式設計-Types

PHP supports the following types:• integer • floating-point numbers • string • array • object

Page 115: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 115

PHP程式設計-Types

Types-integer$a = 1234; # decimal number$a = -123; # a negative number$a = 0123; # octal number

# equivalent to 83 decimal)$a = 0x12; # hexadecimal number (equivalent to 18 decimal)

Page 116: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 116

PHP程式設計-Types

Types-Floating point numbers

$a = 1.234;$a = 1.2e3;

Page 117: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 117

PHP程式設計-Types

Types-Strings If the string is enclosed in double-quotes

("), variables within the string will be expanded (subject to some parsing limitations). As in C and Perl, the backslash ("\") character can be used in specifying special characters

Page 118: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 118

PHP程式設計-Types

Types-Strings(cont)sequence meaning\n newline\r carriage\t horizontal tab\\ backslash\$ dollar sign\" double-quote

Page 119: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 119

PHP程式設計-Types

Types-String conversion

$foo = 1 + "10.5";// $foo is double (11.5)$foo = 1 + "-1.3e3";// $foo is double (-1299)

Page 120: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 120

PHP程式設計-Types

Types-Arrays $a[0] = "abc"; $a[1] = "def"; $b["foo"] = 13;

$a[] = "hello"; // $a[2] == "hello"$a[] = "world"; // $a[3] == "world"

Page 121: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 121

PHP程式設計-Types

Types-Multi-Dimensional Arrays$a[1] = $f; // one dimensional examples$a["foo"] = $f;$a[1][0] = $f; //two dimensional$a["foo"][2] = $f;// (you can mix numeric and associative indices)$a[3]["bar"] = $f;//(you can mix numeric and associative indices)

$a["foo"][4]["bar"][0] = $f; # four dimensional!

Page 122: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 122

PHP程式設計-Types

Types-Multi-Dimensional Arrays(example)# Example 1:

$a["color"] = "red";

$a["taste"] = "sweet";

$a["shape"] = "round";

$a["name"] = "apple";

$a[3] = 4;

Page 123: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 123

PHP程式設計-Types

Types-Multi-Dimensional Arrays(example)# Example 2:

$a = array(

"color" => "red",

"taste" => "sweet",

"shape" => "round",

"name" => "apple",

3 => 4 );

Page 124: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 124

PHP程式設計-Types

Types-Objects class foo {

function do_foo () { echo "Doing foo.";

}}

$bar = new foo;$bar -> do_foo ();

Page 125: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 125

PHP程式設計-Types

Type jugglingPHP does not require (or support) explicit type

definition in variable declaration;

$foo = "0"; // $foo is string (ASCII 48)$foo++; // $foo is the string "1" (ASCII 49)$foo += 1; // $foo is now an integer (2)$foo = $foo + 1.3; //$foo is now a double (3.3)

Page 126: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 126

PHP程式設計-Types

Type casting

The casts allowed are: • (int), (integer) - cast to integer • (real), (double), (float) - cast to double • (string) - cast to string • (array) - cast to array • (object) - cast to object

Page 127: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 127

PHP程式設計Types

Type casting (cont)

$foo = 10; // $foo is an integer$bar = (double)$foo; //$bar is a double

Page 128: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 128

PHP程式設計-VariablesVariable scope $a = 1; /* global scope */Function Test () { echo $a; /*reference to local scope variable */

} Test ();This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope

Page 129: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 129

PHP程式設計-VariablesVariable scope (cont)$a = 1; $b = 2;Function Sum () {

global $a, $b;$b = $a + $b;

} Sum ();echo $b;The above script will output "3". By declaring $a and $b global within the function

Page 130: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 130

PHP程式設計-VariablesVariable scope (cont)$a = 1;$b = 2;

Function Sum () {$GLOBALS["b"] = $GLOBALS["a"] +

$GLOBALS["b"];}

Sum ();echo $b;

Page 131: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 131

PHP程式設計-VariablesVariable scope -static variable Function Test () {

$a = 0;echo $a;$a++;

}This function is quite useless since every time it is called it sets $a to 0 and prints "0"

Page 132: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 132

PHP程式設計-VariablesVariable scope -static variable (cont)Function Test () {

static $a = 0;echo $a;$a++;

}Now, every time the Test() function is called it will print the value of $a and increment it.

Page 133: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 133

PHP程式設計-VariablesVariable scope -static variable (cont)Function Test () {

static $count = 0;$count++;echo $count;if ($count < 10) {

Test ();}$count--;

}The simple function recursively counts to 10

Page 134: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 134

PHP程式設計-VariablesVariable variables$a = "hello";$$a = "world";echo "$a ${$a}"; // Output “hello world”echo "$a $hello"; // Output “hello world”

Page 135: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 135

PHP程式設計-Variables

Variables from outside PHP HTML Forms (GET and POST)Example. Simple form variable<form action="foo.php3" method="post">Name: <input type="text" name="name"><br>

<input type="submit"></form>When submitted, PHP will create the variable $name

Page 136: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 136

PHP程式設計-Variables

Variables from outside PHPExample. More complex form variables<form action="array.php" method="post">Name: <input type="text" name="personal[name]"><br>Email: <input type="text" name="personal[email]"><br>Beer: <br><select multiple name="beer[]">

<option value="warthog">Warthog<option value="guinness">Guinness

</select><input type="submit">

</form>

Page 137: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 137

PHP程式設計-Variables

Variables from outside PHPIMAGE SUBMIT variable names

<input type=image src="image.gif" name="sub">

When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables, sub_x and sub_y.

Page 138: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 138

PHP程式設計-ConstantsPHP defines several constants and provides a

mechanism for defining more at run-time __FILE__

The name of the script file presently being parsed. If used within a file which has been included or required, then the name of the included file is given, and not the name of the parent file. __LINE__

The number of the line within the current script file which is being parsed. If used within a file which has been included or required, then the position within the included file is given.

Page 139: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 139

PHP程式設計-ConstantsPHP_VERSION

The string representation of the version of the PHP parser presently in use; e.g. '3.0.8-dev'. PHP_OS

The name of the operating system on which the PHP parser is executing; e.g. 'Linux'. TRUE

A true value. FALSEA false value.

Page 140: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 140

PHP程式設計-Constants

Example. Defining Constants

<?phpdefine("CONSTANT", "Hello world.");echo CONSTANT; // outputs "Hello world."?>

Page 141: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 141

PHP程式設計-Constants

Example. Using __FILE__ and __LINE__<?phpfunction report_error($file, $line, $message) {

echo "An error occured in $file on line $line: $message.";}

report_error(__FILE__,__LINE__, "Something went wrong!");?>

Page 142: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 142

PHP程式設計-Operators Arithmetic Operators

example name result

$a + $b Addition Sum of $a and $b.

$a - $b Subtraction Remainder of $b subtracted from $a.

$a * $b Multiplication Product of $a and $b.

$a / $b Division Dividend of $a and $b.

$a % $b Modulus Remainder of $a divided by $b.

Page 143: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 143

PHP程式設計-Operators String Operators

There is only really one string operator -- the concatenation operator (".").

$a = "Hello ";$b = $a . "World!";// now $b = "Hello World!"

Page 144: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 144

PHP程式設計-Operators Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to".$a = ($b = 4) + 5;// $a is equal to 9 now, and $b // has been set to 4

Page 145: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 145

PHP程式設計-Operators Assignment Operators (cont)

$a = 3;$a += 5; // sets $a to 8, as if we had

//said: $a = $a + 5;$b = "Hello ";$b .= "There!";

// sets $b to "Hello There!", // just like $b = $b . "There!";

Page 146: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 146

PHP程式設計-Operators Logical Operators example name result

$a and $b And True of both $a and $b are true.

$a or $b Or True if either $a or $b is true.

$a xor $b Or True if either $a or $b is true, but not both.

! $a Not True if $a is not true.

$a && $b And True of both $a and $b are true.

$a || $b Or True if either $a or $b is true.

Page 147: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 147

PHP程式設計-Operators Comparison Operators example name result

$a == $b Equal True if $a is equal to $b.

$a != $b Not equal True if $a is not equal to $b.

$a < $b Less than True if $a is strictly less than $b.

$a > $b Greater than True if $a is strictly greater than $b.

$a <= $b Less than or equal to True if $a is less than or equal to $b.

$a >= $b Greater than or equal to True if $a is greater than or equal to $b.

Page 148: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 148

PHP程式設計-Control Structures ifif ($a > $b) {

print "a is bigger than b";} elseif ($a == $b) {

print "a is equal to b";} else {

print "a is smaller than b";}

Page 149: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 149

PHP程式設計-Control Structures do..while

$i = 0;do {

print $i;} while ($i>0);

Page 150: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 150

PHP程式設計-Control Structures while

$i = 1;while ($i <= 10) {

print $i++; }

Page 151: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 151

PHP程式設計-Control Structures for

for ($i = 1; $i <= 10; $i++) {print $i;

}

for ($i = 1;;$i++) {if ($i > 10) {

break;}print $i;

}

Page 152: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 152

PHP程式設計-Control Structures for

$i = 1;for (;;) {

if ($i > 10) {break;

}print $i;$i++;

}for ($i = 1; $i <= 10; print $i, $i++) ;

Page 153: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 153

PHP程式設計-Control Structures break

break breaks out of the current looping control-structures.$i = 0;while ($i < 10) {

if ($arr[$i] == "stop") {break;

}$i++;

}

Page 154: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 154

PHP程式設計-Control Structures continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration

Page 155: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 155

PHP程式設計-Control Structures switch switch ($i) {

case 0:print "i equals 0"; break;

case 1:print "i equals 1"; break;

case 2:print "i equals 2"; break;

default:print "i is not equal to 0, 1 or 2";

}

Page 156: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 156

PHP程式設計-Control Structures require The require statement replaces itself with the specified file, much like the C preprocessor's #include works. This means that you can't put a requirestatement inside of a loop structure and expect it to include the contents of a different file on each iteration. To do that, use an include statement. require 'header.inc';

Page 157: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 157

PHP程式設計-Control Structures include

$files = array ('first.inc', 'second.inc' , 'third.inc');

for ($i = 0; $i < count($files); $i++) {

include $files[$i];}

Page 158: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 158

PHP程式設計-FunctionsUser-defined functions

function foo ($arg_1, $arg_2, ..., $arg_n) {

echo "Example function.\n";return $retval;

}

Page 159: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 159

PHP程式設計-FunctionsReturning values

function square ($num) {return $num * $num;

}echo square (4); // outputs '16'.

Page 160: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 160

PHP程式設計-FunctionsReturning values You can't return multiple values from a function, but similar results can be obtained by returning a list.function small_numbers() {

return array (0, 1, 2);}list ($zero, $one, $two) = small_numbers();

Page 161: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 161

PHP程式設計-FunctionsFunction arguments

PHP supports passing arguments by value (the default)function takes_array($input) {

echo "$input[0] + $input[1] = ", $input[0]+$input[1];

}

Page 162: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 162

PHP程式設計-FunctionsFunction arguments

Making arguments be passed by referencefunction add_some_extra(&$string) {

$string .= 'and something extra.';}$str = 'This is a string, ';add_some_extra($str);echo $str; // outputs 'This is a string, and something extra.'

Page 163: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 163

PHP程式設計-FunctionsFunction arguments

Making arguments be passed by referencefunction foo ($bar) {

$bar .= ' and something extra.';}$str = 'This is a string, ';foo ($str);echo $str; // outputs 'This is a string, 'foo (&$str);echo $str; // outputs 'This is a string, and something extra.'

Page 164: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 164

PHP程式設計-FunctionsFunction arguments

Default argument valuesfunction makecoffee ($type = "cappucino") {

return "Making a cup of $type.\n";}echo makecoffee ();echo makecoffee ("espresso");

The output from the above snippet is: Making a cup of cappucino.Making a cup of espresso.

Page 165: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 165

PHP程式設計-Classes and ObjectsClass

<?phpclass Cart {

var $items; function add_item ($artnr, $num) {

$this->items[$artnr] += $num;}function remove_item ($artnr, $num) {

if ($this->items[$artnr] > $num) {$this->items[$artnr] -= $num;return true;

} else { return false; } }

}?>

Page 166: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 166

PHP程式設計-Classes and ObjectsClasses are types, that is, they are blueprints for actual variables. You have to create a variables of the desired type with the new operator.

$cart = new Cart;$cart->add_item("10", 1);

Page 167: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 167

PHP程式設計-HTTP authentication

Example. HTTP Authentication example<?phpif(!isset($PHP_AUTH_USER)) {Header("WWW-Authenticate: Basic realm=\"My Realm\"");Header("HTTP/1.0 401 Unauthorized");echo "Text to send if user hits Cancel button\n";exit;

} else {echo "Hello $PHP_AUTH_USER.<P>";echo "You entered $PHP_AUTH_PW as your password.<P>";

}

?>

Page 168: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 168

PHP程式設計-String functions

AddSlashesstring addslashes(string str);Returns a string with backslashes before

characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the null byte)

Page 169: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 169

PHP程式設計-String functions

StripSlashesstring stripslashes(string str);Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes are made into a single backslash

Page 170: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 170

PHP程式設計-String functions

StripSlashes如果各位有使用textarea或text的form時,在輸入中文可能會遇到"許\"或"功\"之類的困擾,因為有些瀏覽器會自動在遇到"\"字元時多加一個"\",或您的MySQL在設定安裝時沒有支援Big5編碼也有此問題. 我們的最終目的是要在中文輸入資料庫中是正常的,不要有多出來或少了"\"字元,在所有可能有中文輸入的form只要做如此處理即可(但這不是最完整的處理方式): $cname=StripSlashes($cname);

Page 171: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 171

PHP程式設計-String functions

printprint(string arg);

output a stringechoecho(string arg1, string [argn]...);

output one or more strings

Page 172: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 172

PHP程式設計-String functions

explode array explode(string separator,

string string);split a string by string splitarray split(string separator, string string);split a string by string

Page 173: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 173

PHP程式設計-String functions

implode (join) string implode(string glue, array pieces);

join array elements with a string. Example:$colon_separated = implode(":", $array);

Page 174: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 174

PHP程式設計-String functions

trim (ltrim ,rtrim)

string trim(string str);

Strip whitespace from the beginning and end of a string.

Page 175: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 175

PHP程式設計-String functions

strlenint strlen(string str);get string length substr

string substr(string string, intstart, int [length]);Return part of a string

Page 176: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 176

PHP程式設計-String functions

strtolowerstring strtolower(string str); Make a string lowercase strtoupper

string strtoupper(string string);Make a string uppercase

Page 177: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 177

PHP程式設計-String functions

str_replace

string str_replace(string needle,string str, string haystack);

Replace all occurrences of needle in haystack with str

Page 178: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 178

PHP程式設計-Regular expression

ereg("abc",$string); Returns true if "abc" is found anywhere in $string.

ereg("^abc",$string);Returns true if "abc" is found at the beginning of $string.

Page 179: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 179

PHP程式設計-Regular expression

ereg("abc$",$string);Returns true if "abc" is found at the end of $string.

$string = ereg_replace("^","<BR>",$string);

Put a <BR> tag at the beginning of $string.

Page 180: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 180

PHP程式設計-FileSystem

fopenint fopen(string filename, string mode);'r' - reading only. 'r+' - reading and writing; 'w' - writing only; 'w+' - reading and writing;'a' - writing only (append)'a+' - reading and writing(append);

Page 181: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 181

PHP程式設計-FileSystem

fwriteint fwrite(int fp, string string, int [length]);Binary-safe file writefgets

string fgets(int fp, int length);get line from file pointer

Page 182: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 182

PHP程式設計-FileSystem

fcloseint fclose(int fp);close an open file pointer

Page 183: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 183

PHP程式設計-FileSystem

example-簡易文字計數器之製作<?php

$counterfile="./counter.txt";

if (file_exists($counterfile)) {

$fp = fopen("$counterfile","r");

$pageviews = fread ($fp,7);

fclose($fp);

$pageviews++;

Page 184: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 184

PHP程式設計-FileSystem

example-簡易文字計數器之製作$fp = fopen("$counterfile","w");

fwrite($fp, $pageviews);

fclose($fp);

} else {

echo " $counterfile not exist!";

fclose($fp); exit;

}

echo "this site has been visited $pageviews times!";

?>

Page 185: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 185

PHP程式設計-FileSystem

example-影像計數器之製作

搭配GD函式庫產生計數器

Page 186: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 186

PHP程式設計- mail

mial()bool mail(string to, string subject, string message, string additional_headers)例如:

$receiver_email = [email protected] ‘; $subject = ’信件傳送測試'; $message = ‘Hello World! '; mail("$receiver_email","$subject","$message");

Page 187: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 187

PHP程式設計- mail

信件的 header (additional_headers)一般的文字可以使用

From: $email\nContent-type: text/plain; charset=big-5\nContent-Transfer-Encoding: 8bit

而 HTML 格式的信件可以使用

From: $email\nContent-Type: text/html; charset=big-5\nContent-Transfer-Encoding: 8bit

Page 188: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 188

PHP程式設計- mail

Example<? $sender= [email protected]';

$receiver= ‘[email protected] '; $subject = ‘HTML格式信件'; $message = ‘<H1> H1字型測試 </H1>'; $e_header = "From: $sender\nContent-Type:

text/html; charset=big-5\nContent-Transfer-Encoding: 8bit"; if (mail("$receiver","$subject","$message"," $e_header")){

print "送信ok! ";} ?>

Page 189: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 189

PHP程式設計-MySQL functions

mysql_connectOpen a connection to a MySQL Server

Descriptionint mysql_connect(string [hostname [:port] [:/path/to/socket] ] , string [username] , string [password] );

Page 190: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 190

PHP程式設計-MySQL functions

mysql_select_dbSelect a MySQL database

Descriptionint mysql_select_db(

string database_name ,int [link_identifier] );

Page 191: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 191

PHP程式設計-MySQL functions

mysql_querySend an SQL query to MySQL

Descriptionint mysql_query(string query, int [link_identifier] );

Page 192: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 192

PHP程式設計-MySQL functions

mysql_num_rowsGet number of rows in result

Descriptionint mysql_num_rows(int result);

Page 193: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 193

PHP程式設計-MySQL functions

mysql_fetch_arrayFetch a result row as an associative array

Descriptionarray mysql_fetch_array(intresult, int [result_type] );

Page 194: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 194

PHP程式設計-MySQL functions

mysql_free_resultFree result memory

Descriptionmysql_free_result(int result)

Page 195: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 195

PHP程式設計-MySQL functions

mysql_closeclose MySQL connection

Descriptionint mysql_close( int

[link_identifier] );

Page 196: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 196

PHP程式設計-MySQL functionsExample: mysql fetch object<?php

mysql_connect($host,$user,$password);

$result = mysql_db_query("database","select * from table" );

while($row = mysql_fetch_array($result)) {

echo $row["user_id"];

echo $row["fullname"];

}

mysql_free_result($result); mysql_close(); ?>

Page 197: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 197

PHP程式設計-MySQL functions

mysql_num_fieldsmysql_field_tablemysql_field_typemysql_field_namemysql_field_lenmysql_field_flags

Page 198: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 198

PHP程式設計-MySQL functionsExample : mysql field types

<?php

mysql_connect("localhost:3306");

mysql_select_db("wisconsin");

$result = mysql_query("SELECT * FROM onek");

$fields = mysql_num_fields($result);

$rows = mysql_num_rows($result);

$i = 0;

Page 199: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 199

PHP程式設計-MySQL functionsExample : mysql field types (cont)

$table = mysql_field_table($result, $i);

echo "Your '".$table."' table has ".$fields;

echo " fields and ".$rows." records <BR>";

echo "The table has the following fields <BR>";

while ($i < $fields) {

$type = mysql_field_type ($result, $i);

$name = mysql_field_name ($result, $i);

Page 200: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 200

PHP程式設計-MySQL functionsExample : mysql field types (cont)

$len = mysql_field_len ($result, $i);

$flags = mysql_field_flags ($result, $i);

echo $type." ".$name." ".$len;

echo " ".$flags."<BR>";

$i++;

}

mysql_close(); ?>

Page 201: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 201

實作範例-SBM管理系統

Page 202: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 202

實作範例-SBM管理系統(軟體管理)

Page 203: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 203

實作範例-SBM管理系統(圖書管理)

Page 204: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 204

實作範例-SBM管理系統(期刊管理)

Page 205: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 205

實作範例-SBM管理系統(系統管理)

Page 206: PHP+MySQL動態網頁設計140.129.118.16/~richwang/Database/PHPnMySQL-DynamicPageDes… · PHP與MySQL的組合既簡單又精彩。PHP有許多管理和 維護MySQL的工具,對MySQL的支持是最全面的。許多有用

2002/9/15 國立中央大學電算中心 陳慶彥 206

實作範例-SBM管理系統

系統實作講解