php & mysql 教學
TRANSCRIPT
112/04/18 PHP5&MySQL程式設計 1
PHP & MySQL 教學
Lesson 1 : PHP + MySQL 導論主講者: appleboy
2009.10.17
112/04/18 PHP5&MySQL程式設計 2
講師介紹
本名:吳柏毅 (Bo-Yi Wu.) My Blog: http://blog.wu-boy.com 現任:
CodeIgniter 繁體中文官方網站 [ 開發 & 維護 ]
PHP-CodeIgniter-Framework-Taiwan 繁體中文翻譯計畫
FreeBSD ports committer, maintainer of several FreeBSD Ports
112/04/18 PHP5&MySQL程式設計 3
Outline
PHP 簡介 PHP 環境建立 PHP 基礎介紹 MySQL 介紹 MySQL 備份、還原 MySQL 四大語法
112/04/18 PHP5&MySQL程式設計 4
What is PHP?
Server 端所使用的嵌入式語言 廣泛使用、多用途的開放源碼腳本語言 內嵌於 HTML(HTML-embedded) Personal Homepage Program
112/04/18 PHP5&MySQL程式設計 5
Who use PHP? 國內各大專院校 ( 台、清、交、成… . 中正、中央 ) 研究機構 ( 中研院、國網中心… )
國網中心 http://www.nchc.org.tw/
營利事業 ( 中華電信、網路家庭… ) 中華電信
http://www.cht.com.tw/index.php 網路家庭
http://www.pchom.com.tw
遊戲論壇 ( 巴哈姆特… ) http://www.gamer.com.tw/
112/04/18 PHP5&MySQL程式設計 6
WHY use PHP ?
最大的優勢 : Free! 免錢 ! 跨平台 (Linux FreeBSD Windows) 學習容易,資源很多 ( http://tw.php.net ) 架設簡單,一分鐘搞定環境? On Windows 網路資料豐富 ( http://www.google.com )
112/04/18 PHP5&MySQL程式設計 7
PHP vs ASP vs JSP ASP
微軟平台 ( 雖然有 apache-asp 平台 ) 效能差 ( 效能輸給 php) 環境架設困難 debug
Jsp 要有 JAVA 基礎 環境架設困難 (Path 路徑 )
112/04/18 PHP5&MySQL程式設計 8
網站運作流程
處理 PHP 轉換
MySQL Server
Web Server PHPUser
request
receive
112/04/18 PHP5&MySQL程式設計 9
環境安裝 Apache PHP MySQL
快速安裝 (1 分鐘 ) Appserv ( http://www.appservnetwork.com/ ) AppServ 2.5.10
Apache 2.2.8 PHP 5.2.6 MySQL 5.0.51b phpMyAdmin-2.10.3
112/04/18 PHP5&MySQL程式設計 10
Appserv 架構
C:\AppServ C:\AppServ\Apache2.2 (Apache 設定檔 ) C:\AppServ\MySQL (MySQL 目錄 )
C:\AppServ\MySQL\data (Database 備份資料 ) C:\AppServ\php5 (PHP library 目錄 ) C:\AppServ\www (Web 主要目錄 http://localhost)
112/04/18 PHP5&MySQL程式設計 11
Apache
DocumentRoot 網站根目錄 DirectoryIndex index.php index.htm VirtualHost <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/usr/local/www/apache22/data/Blog" ServerName blog.wu-boy.com ErrorLog "/var/log/blog.wu-boy.com-error_log" CustomLog /var/log/blog.wu-boy.com-access_log common </VirtualHost>
112/04/18 PHP5&MySQL程式設計 12
PHP
display_errors = On error_reporting = E_ALL & ~E_NOTICE ; default_charset = “iso-8859-1“ ( 註解此行 ) register_globals = Off ( 務必關閉 )
PHP 6 正式移除 magic_quotes_gpc = On
PHP 6 正式移除
112/04/18 PHP5&MySQL程式設計 13
選擇編輯器?
Pspad (http://www.pspad.com/ ) Windows Notepad++ (http://tinyurl.com/2kj23k ) Windows
Ultraedit (http://www.ultraedit.com/ ) Windows Vim (http://www.vim.org/ ) Linux FreeBSD Dreamweaver
112/04/18 PHP5&MySQL程式設計 14
撰寫第一之程式 hello word
<html> <head> <title>First program</title> </head> <body> <? echo "hello, world";?> </body> </html>
112/04/18 PHP5&MySQL程式設計 15
撰寫程式考量
不用的變數盡量不宣告 ( 會警告 ) 變數名稱命名 ( ex: user_id, user_password) 考量程式彈性 ( 不寫死,套變數 ) 縮排、括弧對稱 良好的註解程式
112/04/18 PHP5&MySQL程式設計 16
PHP 註解
可以使用 C, C++, Shell script 形式的註解EX1: /* 這是註解 註解第二行 */
EX2: echo “test”; // 這是註解EX3: echo “test”; # 這是註解
112/04/18 PHP5&MySQL程式設計 17
Use echo
What is different “ and ‘ ? $foo = "foobar"; echo 'foo is $foo'; // foo is $foo echo ”$foo”; // foobar What is different between “ and ‘ ?
112/04/18 PHP5&MySQL程式設計 18
變數與常數
PHP 的變數型別 integer( 整數 ) double( 浮點數 ) string( 字串 ) array( 陣列 ) Boolean object( 物件 ) resource( 資源 ) NULL
112/04/18 PHP5&MySQL程式設計 19
變數指派
使用變數前不需要宣告,指派值的時候 變數自然產生
變數名稱大小寫有差異 變數名稱前需加上” $”
型別轉換 在變數前加上 () , () 內打上想要轉換的型別 只是暫時轉換型別,原變數不會改變型別
112/04/18 PHP5&MySQL程式設計 20
變數
變數的變數: $$var
常數 利用 define 用在固定不會更動的數值( ex: 資料表名稱)
112/04/18 PHP5&MySQL程式設計 21
PHP 常用全域變數
$_POST 儲存經由表單 POST 的表單資訊
$_GET 儲存經由 GET 所得到的資訊
$_SERVER 儲存 Server 端的環境變數
112/04/18 PHP5&MySQL程式設計 22
PHP 常用全域變數 ( 續 )
$_FILES 上傳的檔案資訊
$_SESSION $_COOKIE
112/04/18 PHP5&MySQL程式設計 23
PHP 變數常用函式
isset ( 判斷是變數是否存在,或者為非 null) unset (取消變數 ) empty (判斷是否為空值 )
112/04/18 PHP5&MySQL程式設計 24
PHP算術運算
符號 意義
+ 加法運算
- 減法運算
* 乘法運算
/ 除法運算
% 取餘數
++ 累加
-- 遞減
112/04/18 PHP5&MySQL程式設計 25
字串運算
利用 . echo $a . $b
echo “我是” . “ 小惡魔” $a .= $b;
$a = $a . $b
112/04/18 PHP5&MySQL程式設計 26
設定運算
符號 意義
= 將右邊的值連到左邊 ( 一定要會 )
+= 將右邊的值加到左邊
-= 將右邊的值減到左邊
*= 將左邊的值乘以右邊
/= 將左邊的值除以右邊
%= 將左邊的值對右邊取餘數
.= 將右邊的字串加到左邊
112/04/18 PHP5&MySQL程式設計 27
邏輯運算
符號 意義
< 小於
> 大於
<= 小於或等於
>= 大於或等於
== 等於
!= 不等於
&& and 而且 (And)
|| or 或者 (Or)
xor 互斥 (Xor)
! 不 (Not)
112/04/18 PHP5&MySQL程式設計 28
其它運算符號
符號 意義
$ 變數
& 變數的指標 ( 加在變數前 ) $a = & $b
@ 不顯示錯誤訊息 ( 加在函數前 ) @mysql_query($sql);
-> 物件的方法或者屬性 $a->setType($b)
=> 陣列的元素值 $a = array(“1” => “我” )
? : 三元運算子 ex: echo ($a > $b) ? $a : $b
112/04/18 PHP5&MySQL程式設計 29
流程控制 if
單行 if 多行 else if 省略法 (初學者不建議 ) MVC 寫法:
If($a == $b): elseif: endif;
112/04/18 PHP5&MySQL程式設計 30
while 用於取出 mysql data source 使用
112/04/18 PHP5&MySQL程式設計 31
switch
Switch($mode) {
Case “add”: Break; Case “edit”: Break; Case “delete”: Break;
}
112/04/18 PHP5&MySQL程式設計 32
for
用於處理陣列,或者是連續資料
112/04/18 PHP5&MySQL程式設計 33
foreach
foreach (array as $value) statement;
foreach (array as $key => $value) statement;
例子: foreach ($_POST as $key => $value)
statement;
112/04/18 PHP5&MySQL程式設計 34
require() VS include()
require(): 出現錯誤就中斷程式 include(): 出現錯誤能繼續執行 使用時機?
112/04/18 PHP5&MySQL程式設計 35
switch 迴圈
使用時機
112/04/18 PHP5&MySQL程式設計 36
Function 函式
function myfunc($a, $b) {
// 執行一些動作return $c;
}
112/04/18 PHP5&MySQL程式設計 37
Objects 物件
<?phpclass foo{ function do_foo() { echo "Doing foo."; }}
$bar = new foo;$bar->do_foo();?>
112/04/18 PHP5&MySQL程式設計 38
網址傳值原理 POST & GET global register? PHP6 remove it. $HTTP_GET_VARS = $_GET $HTTP_POST_VARS = $_POST 表單設定
<form method="get"> <form method=“post">
test.php?forum_id=5&sort=add_time ? 掛入變數串 forum_id 、 sort 變數 5 、 add_time 變數的值 & 串起變數
112/04/18 PHP5&MySQL程式設計 39
exercise
N N 乘法表 http://127.0.0.1/example.php?a=9 以上為 99乘法表 http://127.0.0.1/example.php?a=6 以上為 66乘法表
112/04/18 PHP5&MySQL程式設計 40
PHP + MYSQL
My first job? Why use MySQL Database?
112/04/18 PHP5&MySQL程式設計 41
MySQL DataBase
MySQL 的開發理念: MySQL is a relational database management system. MySQL software is Open Source The MySQL Database Server is very fast, reliable, and eas
y to use. Select Insert Update Delete
112/04/18 PHP5&MySQL程式設計 42
phpMyAdmin Web
http://www.phpmyadmin.net MySQL權限與連線設定 預設使用者: root 密碼:空 權限控管: localhost or %
112/04/18 PHP5&MySQL程式設計 43
Navicat for MySQL GUI
http://www.navicat.com/cht
非商業版 ( 免費 ) - Lite 版本
112/04/18 PHP5&MySQL程式設計 44
SQL
MySQL Postgresql Oracle MS SQL Sybase
112/04/18 PHP5&MySQL程式設計 45
Select (查詢 )
Select * from xxx_table bad
Select user_name, user_password from … good
SELECT * FROM table WHERE id = ‘aaa’ and passwd = ‘bbb’
Left join, right join, GROUP, order by Sub query
112/04/18 PHP5&MySQL程式設計 46
Insert into
INSERT INTO 資料表 ( 欄位 1, 欄位 2, 欄位 3) VALUES (‘1’, ‘2’, ‘3’)
欄位全部用上,可省略欄位名稱 流水號欄位,不用寫入
112/04/18 PHP5&MySQL程式設計 47
Delete 刪除
Delete from table where id = ‘’ Delete from table where id in (‘1’, ‘2’)
Implode $del_id = implode(",", $array);
112/04/18 PHP5&MySQL程式設計 48
Update
UPDATE table SET 欄位 1 = ‘ 資料’ , 欄位 2 = ‘ 資料’ , WHERE 欄位 = ‘ 資料’
112/04/18 PHP5&MySQL程式設計 49
Create 資料表
Use phpMyAdmin CREATE TABLE `degree_map` (
`code` char(1) NOT NULL default '', `title` tinytext NOT NULL, PRIMARY KEY (`code`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
112/04/18 PHP5&MySQL程式設計 50
Drop table 刪除資料表
DROP TABLE tbl_name tbl_name 資料表
DROP DATABASE db_name
112/04/18 PHP5&MySQL程式設計 51
MySQL 備份、恢復
Use mysqldum command to backup your database
User mysql command to restore your database Mysqldump –u root –p database_name > db.sql Mysql –u root –p database_name < db.sql --default-character-set=latin1, utf8
112/04/18 PHP5&MySQL程式設計 52
exercise
Use phpMyAdmin Create one database Create table project_news
news_id news_title news_desc add_time edit_time
112/04/18 PHP5&MySQL程式設計 53
html
CSS, javascript, html Form 表單
112/04/18 PHP5&MySQL程式設計 54
Exercise & Homework
留言板系統 show.php 取出資料 add.php 新增資料 other.php 修改刪除資料
Database 規劃