当前位置: 移动技术网 > IT编程>开发语言>PHP > Pear DB 新手入门指南教程第1/3页

Pear DB 新手入门指南教程第1/3页

2019年05月09日  | 移动技术网IT编程  | 我要评论
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

 
<?php
//examples here
?>
 

 

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网