txtsql的最大優點之一是文檔很詳細,可惜,我在網上找了半天也找不到中文版的文檔,所以只好自己動手,利人利已吧,不過自己的E文水平自己是很清楚的,希望大家看了不會笑掉大牙才好,還希望大家多多指教。
歡迎使用txtSQL 2.2快速安裝手冊。這頁將指引你如何開始安裝txtSQL。
1-解壓縮下載包
2-配置類檔案
2.1-目錄結構
3-包含類檔案
3.1-類執行個體
3.2-串連到txtSQL
3.3- 更改密碼
3.4-選擇一個資料庫
4-執行SQL指令
4.1-指令的列表
4.2-顯示結果
5-從斷開txtSQL串連
6-差錯處理
7-發行的txtSQL函數
1、解壓縮下載包
當你開啟.zip檔案時,你將注意到有兩個檔案: txtSQL.class.php和txtSQL.core.php。提取兩個檔案到相同的目錄。建立一個任意名字的新目錄; 通常,它名為data。這將是包含資料庫的目錄。它能可以放在伺服器上的任何地方,但是它通常位於以上兩個檔案的同一目錄下。確保這個目錄許可權是0755或者更高。現在返回到.zip檔案找到\'txtsql.MYI\'提取它到我們剛剛建立的資料庫目錄。(譯者註:其實不用這麼麻煩,.zip檔案已經組織好了,全部解壓到伺服器上的任意目錄,並設定許可權就行了)
2、配置類檔案
使用txtSQL的第一步,配置類檔案,這樣它才能被包含到可能要求它的php檔案中。首先,你必須在文字編輯器中開啟檔案txtSQL.class.php 開啟檔案時將注意到一個著作權聲明,其後是一些其它素材。隨後有這樣一行(預設是第30行):
30. include_once(\'./txtSQL.core.php\');
這一行代碼使它包括txtSQL的的核心函數和類。方便php找到核心檔案,你必須編輯單引號內的內容,讓它指向txtSQL.core.php檔案。(譯者註:這個基本上也不用設定,源檔案已經配置好了!只有當你的檔案不在同一目錄時,才需要這麼做)
2.1、目錄結構
一個有效資料庫目錄結構應該是這樣的:
+ datafolder (所有資料庫的儲存目錄,比如上面建立的\'data\' )
+ database_name
+ table.FRM (列定義)
+ table.MYD (行資料)
+ txtsql
+ txtsql.MYI (包含在壓縮包)
基本上,一個資料庫是主要的資料庫目錄下的一子目錄。
同時在資料庫目錄內部是txtsql資料庫,壓縮包中的\'txtsql.MYI\'I。
在所有的資料庫內部,一個資料表由兩個檔案組成; table.FRM,和table.MYD。.FRM是列定義,另一個是資料行。
3、包含類檔案
現在我們已經配置完txtSQL2.2,我們能開始使用它。首先使用文字編輯器創造一個空白的php檔案。儲存為example.php。
為了簡單的說明,假設你把它儲存在和\'txtSQL.class.php\'同樣的目錄下。
現在我們必須包括php類,在\'example.php中輸入:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); ?> |
3.1類執行個體
在物件導向編程( OOP)中,當建立類時,一種特殊變數類型--個對象是自動地創造。
我們需要創造指向txtSQL類的一個對象,那麼把這些添加到檔案:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); ?> |
在單引號中的文字,是包含所有資料庫的資料目錄的路徑。這個目錄下必須包含一個txtsql(大小寫敏感 )的目錄,目錄下應該有一個\'txtsql.MYI\'的檔案。這個檔案包含操作資料庫所有使用者與和密碼。
這個目錄與檔案已經在txtSQL壓縮包中。一旦路徑是正確的,你可以繼續向前到下一段。
3.2串連資料庫
現在我們可以用正確的使用者名稱和密碼來串連資料庫了。
預設的使用者名稱是root\',預設的密碼是空。(強烈建議在下面的步驟中修改)
用下面的代碼來串連資料庫:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // 預設時是 $sql->connect(\'root\', \'\'); ?> |
txtSQl這時會認可你是它的使用者,准許你訪問資料庫和表。
注意:參考手冊中有可用的命令清單。
3.3、更改密碼
如果你想變更管理員密碼(root),可以用grant_permissions() 函數,grant_permissions() 函數這樣調用:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->grant_permissions($action, $user, $pass [, $newpass]); |
?>參數 $action(動作)可以是 add(添加), drop(刪除), or edit(編輯). $newpass(新密碼)只有在你編輯(edit)使用者時才可用。
$user(使用者)是用你要操作的使用者名稱, $pass是它的密碼。
例如, 如果你想改變使用者\'root\'的密碼為 \'bar\' (假設它還是空的), 我們可以這麼做:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->grant_permissions(\'edit\', \'root\', \'\', \'bar\'); ?> |
或者
建立一個使用者 \'foo\' 密碼為\'bar\'
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->grant_permissions(\'add\', \'foo\', \'bar\'); ?> |
或者
刪除一個使用者\'foo\' 密碼為 \'bar\'
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->grant_permissions(\'drop\', \'foo\', \'bar\'); ?> |
注意:你不用刪除使用者root\',如果沒有正確的密碼你也不能訪問任何資料。
3.4、選擇資料庫
像mySQL一樣, 在操作一個資料表之前,你必須先說明它在哪一個資料庫. 這個步驟不是必須的,因為你可以在操作時指定使用哪一個資料庫.
我們使用下面的語句來選擇一個資料庫:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->selectdb(\'test\'); //選擇了資料庫 \'test\' ?> |
4、執行指令
通常我們只要使用$sql對象的各種方法下執行指令。
例如:
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->selectdb(\'test\'); // 選擇了資料庫 \'test\' $results = $sql->select(array( \'db\' => \'test\', //這行不是必須的,因為我們已經選定了資料庫 \'table\' => \'test\', \'where\' => array(\'id = 10\', \'and\', \'name =~ John Smith\'), \'limit\' => array(0, 100) )); ?> |
4.1、指令列表
txtSQL2.2支援的指令如下:
以下為引用的內容: 4.1- List of commands showdbs() createdb() dropdb() renamedb() select() insert() update() delete() showtables() createtable() droptable() altertable() describe() |
在執行指令之前,你必須串連資料庫,不然會產生錯誤。手冊中會用詳細的指令說明和執行個體(隨後翻譯)。
4.2、顯示結果
$results變數現在包含了表test\'中選中行的資訊。
你可以用一個迴圈來實現顯示$results中的所有結果。
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->selectdb(\'test\'); // database \'test\' is now selected $results= $sql->execute(\'select\', array(\'select\' => array(\'id\', \'name\'), \'db\' => \'test\', \'table\' => \'test\', \'where\' => array(\'id = 10\', \'and\', \'name =~ John Smith\'), \'limit\' => array(0, 100)))); foreach ( $results as $key => $row ) { print \"ID: $row[id], NAME: $row[name]<BR>\n\"; } ?> |
5-斷開txtSQL
用完之後斷開資料庫是一個好習慣。斷開用 disconnect()函數。
以下為引用的內容: <?php include(\'./txtSQL.class.php\'); $sql = new txtSQL(\'./data\'); $sql->connect($username, $password); // default is $sql->connect(\'root\', \'\'); $sql->selectdb(\'test\'); // database \'test\' is now selected $results= $sql->execute(\'select\', array(\'select\' => array(\'id\', \'name\'), \'db\' => \'test\', \'table\' => \'test\', \'where\' => array(\'id = 10\', \'and\', \'name =~ John Smith\'), \'limit\' => array(0, 100)))); foreach ( $results as $key => $row ) { print \"ID: $row[id], NAME: $row[name]<BR>\n\"; } $sql->disconnect(); ?> |
6-錯誤處理
txtSQL 包含錯誤處理能力。主要用以下的函數:
以下為引用的內容: strict() get_last_error() last_error() errordump() |