6 | $link = @ mysql_connect ( "localhost" , "encryptdata" , "0000" ) or die ( "mysql worng - " . mysql_error ()); |
完整程式檔案請參考 encryptor_function.php 。
內建函數 (function) mysql_connect() 用來跟 MySQL 資料庫連線,共需要三個參數 (parameter) ,第一個參數為伺服器種類,第二個參數為使用者帳號,第三個參數則是使用者密碼。後面的 or 為關鍵字 (keyword) 之一,此為邏輯運算子進行「或」運算,因此如果 mysql_connect() 得到 false ,也就是無法登錄 MySQL 資料庫就會執行其後的部份。
die() 用為顯示錯誤訊息,至於 mysql_error() 則會回傳 MySQL 資料庫出錯的地方。
由於 $link 亦為全域變數 (global variable) ,因此有用到 global 的函數如 new_button() 、 load_button() 、 encode_button() 、 decode_button() 、 clean_button() 都要加入 $link ,例如 new_button() 這一行就要改成
48 | global $e , $data , $link ; |
因為執行這五個函數也都要寫進資料庫中,因此 new_button() 要加進
54 55 | $sql = "insert into resord (action, code, input, output) values('new', '{$s}', '', '')" ; mysql_db_query ( "encrypt_software" , $sql , $link ) or die ( "mysql wrong" ). mysql_error (); |
變數 $sql 為 SQL 語法字串 (string) ,我們可以看到語法就是
54 | insert into resord (action, code, input, output) values('new', '{$s}', '', '') |
insert into 後面接資料表 (data table) 名稱 resord ,再後面用小括弧圍起來的是欄位,除了 sn 之外,我們其他四個欄位都有寫值進去,寫進去的值就是字串,其中 code 部份是用大括弧圍起來的變數 $s 。
下一行, mysql_db_query() 連結資料庫處理資料的函數,共需三個參數,第一個參數為資料庫名稱,第二個參數為 SQL 語法,第三個參數則是連線的變數 $link 。
其他四個函數 load_button() 、 encode_button() 、 decode_button() 、 clean_button() 也雷同,請直接參考 encryptor_function.php 。
寫入資料庫的部份加進去之後,我們接著要完成 record() 與 about() 兩個函數。 record() 的設計如下
131 132 133 134 135 136 137 138 139 140 141 | function record() { global $link ; $sql = "select * from resord" ; $result = mysql_db_query ( "encrypt_software" , $sql , $link ) or die ( "mysql wrong" ). mysql_error (); $temp = "<h3>Record:</h3><br / >" ; $temp = $temp . "sn-action-input-output<br / >" ; while ( $data = mysql_fetch_array ( $result )) { $temp = $temp . $data [ 'sn' ]. "-" . $data [ 'action' ]. "-" . $data [ 'input' ]. "-" . $data [ 'output' ]. "<br / >" ; } return $temp ; } |
因為 record() 是要從資料庫中擷取資料,然後把資料顯示在 $main 裡,所以用的 SQL 語法為 select
133 | $sql = "select * from resord" ; |
星號 * 表示所有的欄位, from 後面接資料表名稱。這裡同樣用 mysql_db_query() 處理資料,不過這次是回傳資料庫資料到變數 $result 之中,下面的 $temp 為建立顯示資料的字串,最後也是回傳 $temp ,留意這一行
136 | $temp = $temp . "sn-action-input-output<br / >" ; |
由於 $temp 是字串,因此這一行是 $temp 連接新的字串後,再把值傳回 $temp ,其實也可以直接用 .= 運算子。
底下的 while 迴圈 (loop) 逐次處理由資料表 resord 取出的資料,內建函數 mysql_fetch_array() 會依順序將資料取出來放到陣列中。
至於 about() 就回傳版權資料字串,如下
143 144 145 146 147 148 149 150 151 | function about() { $temp = "《程式語言教學誌》的範例程式<br / > 名稱:Encrypt Software<br / > 功能:示範 PHP 程式<br / > 作者:張凱慶<br / > 時間:西元 2012 年 11 月 */"; return $temp ; } |
來跑跑看囉! Record 頁

About 頁

好了,網站大抵完成了,下一步是?
中英文術語對照 | |
---|---|
資料庫 | database |
變數 | variable |
函數 | function |
參數 | parameter |
關鍵字 | keyword |
全域變數 | global variable |
字串 | string |
資料表 | data table |
迴圈 | loop |
您可以繼續參考
資料庫篇
相關目錄
回 PHP 入門指南
回 PHP 教材
回首頁
參考資料
http://www.phpmyadmin.net/home_page/index.php
http://dev.mysql.com/doc/refman/5.5/en/tutorial.html
http://php.net/manual/en/function.die.php
http://php.net/manual/en/function.mysql-connect.php
http://php.net/manual/en/function.mysql-db-query.php
http://php.net/manual/en/function.mysql-error.php
http://php.net/manual/en/function.mysql-fetch-array.php
沒有留言:
張貼留言