Pear DB 入门指南

4. 可用方法列表

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

5. 错误处理机制
5.1. 从Pear DB Error获得错误信息
所有从Pear DB 返回的错误都是Pear Errors. 这有一种方法来搜集:

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

5.2 Debug Pear DB Errors
Pear DB采用一种轻便的错误消息系统向用户报错。把错误信息简单翻译成其它语言或者对于一种特殊错误采取特殊的处理方式这都带来了很大的优点。但是对于开发人员来说这些提示并么有提供很有用的信息。想要得到真实的数据处理出错的信息,你可以使用getDebugInfo()方法:

<?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];
 }
 ?>

通过当一个PHP函数出错时,会打印出出错提示。在pear中的这种机制被屏蔽了。但时有时你可能需要在代码中捕捉一些错误信息。可以使用set_error_handler PHP 函数, 从 PHP Manual获取信息.简单示例:

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

5.3 对错误采取自动处理
正如你所看见的, Pear DB提供了广泛的错误检测和报告机制,这强迫开发人员必需对返回的数据结果进行检查,是否有错。 Pear DB同时照顾我们避免这种痛苦的工作,提供了一种灵活的体系,在一个错误出现的时候自动调用相应的措施。
这些可能的措施包括:

返回错误对象 (PEAR_ERROR_RETURN). 这是默认的.
打印错误 (PEAR_ERROR_PRINT)
打印错误信息并忽略执行(PEAR_ERROR_DIE)
用PHP函数 trigger_error()来列举错误(PEAR_ERROR_TRIGGER)
把错误对象传递给一个函数或者类的方法 (PEAR_ERROR_CALLBACK)

简单示例:

<?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 (
     0 => <column "id" data>,
     1 => <column "name" data>,
     2 => <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;
 ?>

高级示例:

<?php
 ...
 // 1) Set the mode per call:
 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
     [..]
 }
 while ($result->fetchInto($row, DB_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'];
 }
 ?>

下面为扩展错误机制提供了一个很好的想法:

<?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)) {
         break;
     }
     $id = $row[0];
     ....
 }
 ?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据