Pear DB 新手入門指南教程第1/3頁_PHP教程

來源:互聯網
上載者:User

1. 簡介這是一部指導我們如何使用Pear DB擴充。Pear DB,提供這樣一系列的類:
n 資料庫抽象
n 進階錯誤處理機制
n 以及其它

2. 下載、安裝Pear
由於現在Pear項目仍處於緊鑼密鼓的開發之中,所以得到它的最好辦法就是從CVS獲得(Pear DB發行包已經跟隨PHP4.0.6以後版本捆綁發布)。所以,我們只需要把Pear的根目錄放到php.ini設定檔include_path中。也可以通過這樣設定: _set('include_path', '/pear_base_dir').

以下是strp by step樣本:

存放Pear的目錄:
# cd /usr/local/lib
用“phpfi“口令登入:
# cvs -d :pserver:cvsread@cvs.php.net:/repository login
用以下命令得到所有的pear檔案,同時也可以用來更新已經下載的檔案。其他的參數有:"today", "last month",等。我推薦用"last week"參數,因為一般bugs的提交和修改都是每周一次。 
# cvs -d :pserver:cvsread@cvs.php.net:/repository export -D "last week" php4/pear
編輯php.ini檔案加上下面一段在include_path處: /usr/local/lib/php4/pear 如果沒有修改的許可權,可以通過這條語句在代碼中實現: ini_set('include_path', 'path_to_pear');

獲得PHP CVS的完全文檔

注意Pear DB必需PHP版本4.0.4以上,而在Pear中的一些其他包如:XML Parser of the pear installer script需要PHP4.0.5以上版本。

3. 使用Pear DB

3.1 串連,斷開資料庫

 

// The pear base directory must be in your include_path
require_once 'DB.php';
$user = 'foo';
$pass = 'bar';
$host = 'localhost';
$db_name = 'clients_db';

// Data Source Name: This is the universal connection string
$dsn = "mysql://$user:$pass@$host/$db_name";

// DB::connect will return a Pear DB object on success
// or a Pear DB Error object on error
// You can also set to TRUE the second param
// if you want a persistent connection:
// $db = DB::connect($dsn, true);
$db = DB::connect($dsn);

// With DB::isError you can differentiate between an error or
// a valid connection.
if (DB::isError($db)) {
die ($db->getMessage());
}
....
// You can disconnect from the database with:
$db->disconnect();
?>
 

資料來源(上例中的$dsn 參數)有以下允許的格式:(從Pear/DB.php的parseDSN方法複製而來)

 
     *  phptype: Database backend used in PHP (mysql, odbc etc.)
* dbsyntax: Database used with regards to SQL syntax etc.
* protocol: Communication protocol to use (tcp, unix etc.)
* hostspec: Host specification (hostname[:port])
* database: Database to use on the DBMS server
* username: User name for login
* password: Password for login
*
* The format of the supplied DSN is in its fullest form:
*
* phptype(dbsyntax)://username:password@protocol+hostspec/database
*
* Most variations are allowed:
*
* phptype://username:password@protocol+hostspec:110//usr/db_file.db
* phptype://username:password@hostspec/database_name
* phptype://username:password@hostspec
* phptype://username@hostspec
* phptype://hostspec/database
* phptype://hostspec
* phptype(dbsyntax)
* phptype

現在支援的資料庫有 (在 phptype DSN 部分):

 
mysql  -> MySQL
pgsql -> PostgreSQL
ibase -> InterBase
msql -> Mini SQL
mssql -> Microsoft SQL Server
oci8 -> Oracle 7/8/8i
odbc -> ODBC (Open Database Connectivity)
sybase -> SyBase
ifx -> Informix
fbsql -> FrontBase

注意並不是所有資料庫特徵都支援,可以從根目錄>/DB/STATUS 得到詳細的清單。

3.2 執行資料庫

 

// Once you have a valid DB object
...
$sql = "select * from clients";
// If the query is a "SELECT", $db->query will return
// a DB Result object on success.
// Else it simply will return a DB_OK
// On failure it will return a DB Error object.
$result = $db->query($sql);
// Always check that $result is not an error
if (DB::isError($result)) {
die ($result->getMessage());
}
....
?>
 

3.3 獲得select的資料

http://www.bkjia.com/PHPjc/319757.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319757.htmlTechArticle1. 簡介這是一部指導我們如何使用Pear DB擴充。Pear DB,提供這樣一系列的類: n 資料庫抽象 n 進階錯誤處理機制 n 以及其它 2. 下載、安裝...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.