1. 简介这是一部指导我们如何使用pear db扩展。pear db,提供这样一系列的类: n 数据库抽象 n 高级错误处理机制 n 以及其它 2. 下载、安装pear
<?php // untested code !!! // // example inserting data $alldata = array( array(1, 'one', 'en'), array(2, 'two', 'to'), array(3, 'three', 'tre'), array(4, 'four', 'fire') ); $sth = $dbh->prepare("insert into numbers values( , , )"); foreach ($alldata as $row) { $dbh->execute($sth, $row); } //here's an example of a file placeholder: $myfile = "/tmp/image.jpg"; $sth = $dbh->prepare('insert into images ( , &)'); $dbh->execute($sth, array("this is me", $myfile)); //after i commit a bugfix that i have on my laptop, you can use //parameter arrays in the getxxx methods too: $ver = $dbh->getone("select stableversion from packages where name = ", array($package)); ?> |
3.8 autocommit, commit and rollback
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(); } ?> |
4.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 // 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'); ... ?> |
3
如您对本文有疑问或者有任何想说的,请
点击进行留言回复,万千网友为您解惑!
相关文章:
-
-
-
-
-
-
-
七种PHP开发环境搭建工具
对于php开发小白来说搭建一个php运行环境就是一道坎!因为要做php开发,搭建一个能够运行php网站的服务器环境是第一步,传统的php环境软件非常复杂,好在很...
[阅读全文]
-
PHP实现抽奖功能实例代码
在项目开发中经常会遇到花钱抽奖类型的需求。但是老板总是担心用户用小钱抽到大奖。这样会导致项目亏损。下边这段代码可以有效制止抽奖项目亏钱。个人奖池:语言:thin...
[阅读全文]
-
-
-
PHP 超级全局变量相关总结
超级全局变量在php 4.1.0之后被启用, 是php系统中自带的变量,在一个脚本的全部作用域中都可用。php 超级全局变量php中预定义了几个超级全局变量(s...
[阅读全文]
-
网友评论