4. 可用方法列表
<?php /* * From the DB_(driver) objects */ // get the object with, ie: $db = DB::connect('mysql://user:pass@localhost/my_db'); // Set options $db->setErrorHandling(); $db->setFetchmode(); // Information $db->affectedRows(); $db->tableInfo(); // Database manipulation $db->query(); // Data fetch $db->nextId(); $db->getOne(); $db->getRow(); $db->getCol(); $db->getAssoc(); $db->getAll(); // Place holders and execute related $db->quote(); $db->prepare(); $db->execute(); $db->executeMultiple(); // Transactions $db->autoCommit(); $db->commit(); $db->rollback(); // Disconnection $db->disconnect(); /* * From DB_result objects */ // get the object with, ie: $res = $db->query('select * from foo'); // Data fetch $res->fetchRow(); $res->fetchInto(); // Result Info $res->numCols(); $res->numRows(); $res->tableInfo(); // Free $res->free(); /* * From DB_error objects */ // get the object with, ie: $error = $db->query('select * from no_table'); $error->getMessage(); $error->getDebugInfo(); $error->toString(); ?> |
5. 错误处理机制
5.1. 从Pear DB Error获得错误信息
所有从Pear DB 返回的错误都是Pear Errors. 这有一种方法来搜集:
<?php ... $res = $db->query('select * from no_table'); if (DB::isError($res)) { // get the portable error string echo $res->getMessage(); } ?> |
5.2 Debug Pear DB Errors
Pear DB采用一种轻便的错误消息系统向用户报错。把错误信息简单翻译成其它语言或者对于一种特殊错误采取特殊的处理方式这都带来了很大的优点。但是对于开发人员来说这些提示并么有提供很有用的信息。想要得到真实的数据处理出错的信息,你可以使用getDebugInfo()方法:
<?php $sql = 'select * from no_table'; if (DB::isError($res = $db->query($sql))) { // get the native backend error // and the last query echo $res->getDebugInfo(); } ?> |
通过当一个PHP函数出错时,会打印出出错提示。在pear中的这种机制被屏蔽了。但时有时你可能需要在代码中捕捉一些错误信息。可以使用set_error_handler PHP 函数, 从 PHP Manual获取信息.简单示例:
<?php // what messages to report error_reporting (E_ALL ^ E_NOTICE); // this function will handle all reported errors function my_error_handler ($errno, $errstr, $errfile, $errline) { echo "In $errfile, line: $errline\n $errstr"; } set_error_handler ('my_error_handler'); $db = DB::connect('pgsql://postgres@localhost/no_db'); ... ?> |
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 require_once 'DB.php'; // Set the default action to take on error PEAR::setErrorHandling(PEAR_ERROR_DIE); // From here you don't need to check errors any more $db = DB::connect('pgsql://postgres@localhost/my_database'); $res = $db->query('select id from no_table'); // at this point the execution is aborted and the error message is raisen ... ?> |
高级示例:
<?php // Define the app environment (this is: what errors you want to output) define ('DEBUG_ENV', true); // This function will handle all errors function handle_pear_error ($error_obj) { // Be verbose while developing the application if (DEBUG_ENV) { die ($error_obj->getMessage()."\n".$error_obj->getDebugInfo()); // Dump a silly message if the site is in production } else { die ('Sorry you request can not be processed now. Try again later'); } } require_once 'DB.php'; // On error, call the "handle_pear_error" function back // You can also use an object as pear error handler so: // setErrorHandling(PEAR_ERROR_CALLBACK, array($object,'method_name'); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_pear_error'); $db = DB::connect('pgsql://postgres@localhost/site_db'); $res = $db->query('select id from no_table'); // at this point the execution is aborted and the "handle_pear_error" // function is called with the error object as its first argument while ($row = $res->fetchRow()) { ... } ... ?> |
下面为扩展错误机制提供了一个很好的想法:
<?php error_reporting (E_ALL ^ E_NOTICE); // this function will handle all errors reported by PHP function php_error_handler ($errno, $errstr, $errfile, $errline) { die ("In $errfile, line: $errline\n $errstr"); } set_error_handler ('php_error_handler'); // this function will catch errors generated by Pear, // transform it to PHP errors and trigger them to the php_error_handler function pear_error_handler ($err_obj) { $error_string = $err_obj->getMessage() . ' ' . $error_obj->getDebugInfo(); trigger_error ($error_string, E_USER_ERROR); } require 'DB.php'; PEAR::setErrorHandling (PEAR_ERROR_CALLBACK, 'pear_error_handler'); // force an error $db = DB::connect('pgsql://postgres@localhost/no_db'); ... ?> |