Pear DB 新手入门指南

本文档为PearDB的新手提供了一套全面的指南,包括下载、安装PearDB的方法,以及如何利用PearDB进行数据库连接、执行查询操作等内容。此外,还介绍了如何处理查询结果和PearDB的错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pear DB 新手入门指南
(原文:《 Quick Start Guide to Pear DB 》)
翻译:陈刚 (GangChen@wiscom.com.cn)
 
    
1.        简介-------------------------------------------------------------------------------------2
2.        下载、安装Pear-------------------------------------------------------------------------2
3          使用Pear DB----------------------------------------------------------------------------2
3.1.1   连接,断开数据库
3.1.2   执行数据库
3.1.3   获得 select 的数据
3.1.3.1  获取数据的函数
3.1.3.2  选择获取数据的格式
3.1.3.3  设置获取数据的格式
3.1.3.4  控制获取数据数量
3.1.3.5  清除结果,释放变量
3.1.4   快速 retrieve 数据
3.1.5   从查询结果获得更多信息 (numRows, numCols, affectedRows, tableInfo)
3.1.6   自动增长( Sequences
3.1.7   Prepare & Execute/ExcuteMultiple
3.1.8   autoCommit, commit and rollback
4.        可用方法列表---------------------------------------------------------------------------10
5          错误处理机制---------------------------------------------------------------------------12
5.1         Pear DB Error 获得错误信息
5.2         Debug Pear DB Errors
5.3        对错误采取自动处理

这是一部指导我们如何使用 Pear DB 扩展。 Pear DB ,提供这样一系列的类:
n          数据库抽象
n          高级错误处理机制
n          以及其它
 
由于现在 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');
注意 Pear DB 必需 PHP 版本 4.0.4 以上,而在 Pear 中的一些其他包如: XML Parser of the pear installer scrīpt 需要 PHP4.0.5 以上版本。
 
3.1         连接,断开数据库
 
<?php
// 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
注意并不是所有数据库特征都支持,可以从 <pear 根目录 >/DB/STATUS 得到详细的清单。
3.2         执行数据库
 
<?php
// 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());
}
....
?>
 
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值