首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
lzmtw (32)
longrujun (79)
daisylh (21)
yjz0065 (122)
superyan (28)
cyz1980 (103)
sharpdew (1)
psyl (155)
Kendiv (119)
bz201 (41)
CSDN - 文档中心 - 其他开发语言 阅读:329   评论: 0    参与评论
标题   Pear DB 新手入门指南     选择自 flappy_man 的 Blog
关键字   Pear DB 新手入门指南
出处  

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        对错误采取自动处理


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         连接,断开数据库

 
<?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.phpparseDSN方法复制而来)

 
     *  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         执行数据库

 
<?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());
}
....
?>
 

 

3.3         获得select的数据

3.3.1   获取数据的函数

 
<?php
// Once you have a valid DB Result object
...
// Get each row of data on each iteration until
// there is no more rows
while ($row $result->fetchRow()) {
    
$id $row[0];
}
?>
 

除了fetchRow()还可以使用fetchInto()直接插入$row的值。

 
<?php
...
while (
$result->fetchInto($row)) {
    
$id $row[0];
}
?>
 

 

3.3.2   选择获取数据的格式

获取模式有DB_FETCHMODE_ORDERED(默认), DB_FETCHMODE_ASSOC and DB_FETCHMODE_OBJECT.

从获取数据方法返回的结果示例:

 
<?php
$res 
$db->query('select id, name, email from users');
$row $res->fetchRow($mode);

//With $mode = DB_FETCHMODE_ORDERED
//The default behavior is to return an ordered array.
$row = array (
    
=> <column "id" data>,
    
=> <column "name" data>,
    
=> <column "email" data>
);

$id $row[0];

//With $mode = DB_FETCHMODE_ASSOC
//Returns an associative array with column names as array keys:
$row = array (
    
'id'    => <column "id" data>,
    
'name'  => <column "name" data>,
    
'email' => <column "email" data>
);

$id $row['id'];

//With $mode = DB_FETCHMODE_OBJECT
//Returns a DB_row object with column names as properties:
$row db_row Object 
(
    [
id]    => <column "id" data>,
    [
name]  => <column "name" data>,
    [
email] => <column "email" data>
)

$id $row->id;
?>
 

 

3.3.3   设置获取数据的格式

可以使用 fetchrow() / fetchInto() 方法或者为你的DB实例设置一个默认的模式。

 
<?php
...
// 1) Set the mode per call:
while ($row $result->fetchRow(DB_FETCHMODE_ASSOC)) {
    [..]
}
while (
$result->fetchInto($rowDB_FETCHMODE_ASSOC)) {
    [..]
}

// 2) Set the mode for all calls:
$db DB::connect($dsn);
// this will set a default fetchmode for this Pear DB instance
// (for all queries)
$db->setFetchMode(DB_FETCHMODE_ASSOC);
$result $db->query(...);
while (
$row $result->fetchRow()) {
    
$id $row['id'];
}
?>
 

 

3.3.4   控制获取数据数量

同时Pear DB获取数据可以带有额外的参数,可以使用一个数字参数来获取需要的数据数量。在你只需要获得数据中的一部分时这时候特别有用(比如在做分页程序的时候)

 
<?php
...
// the row to start fetching
$from 50;
// how many results per page
$res_per_page 10;
// the last row to fetch for this page
$to $from $res_per_page;
foreach (
range($from$to) as $rownum) {
    if (!
$row $res->fetchrow($fetchmode$rownum