谢谢,雪悟,二球.

Pear DB 新手入门指南4

上一篇 / 下一篇  2006-12-18 17:55:03 / 个人分类:全是帅的不能再输啦

4.       可用方法列表PHPChina 开源社区门户$X,mNIay%d3Q K O)_

 
<?php
HzA1X"X%T$n U0
/*
L0Ehn&IB@a:W0* From the DB_(driver) objects
"l6S$mvu0*/PHPChina 开源社区门户"X$L8aw|+}
// get the object with, ie:
JH @8K X9`}:E |0
$db DB::connect('mysql://user:pass@localhost/my_db');PHPChina 开源社区门户dww8y!K*Hj
 PHPChina 开源社区门户 x?FP P|!g n @6R
// Set optionsPHPChina 开源社区门户rs#|/z,@5h
$db->setErrorHandling();PHPChina 开源社区门户 ] TNOY9F@Jigd
$db->setFetchmode();
1UQ `hR"n7~0
// InformationPHPChina 开源社区门户1SM.f1m'GH\7c
$db->affectedRows();
e5Ff/|5r |T0
$db->tableInfo();PHPChina 开源社区门户^)R9Kw)x"B Ub
// Database manipulation
"Y7X5v"n N+@(yNK0
$db->query();
v$T%bo*X+eM0
// Data fetch
a3i$n4{"g0
$db->nextId();PHPChina 开源社区门户%H#D_s7X#H-Zts$[
$db->getOne();
x7n O"m^&h)Wnn0
$db->getRow();PHPChina 开源社区门户do eCf#[_N s/gQz(l
$db->getCol();
@5t)lq/tSB(Z`6^0
$db->getAssoc();
:UI/n1}2lF(w E0
$db->getAll();PHPChina 开源社区门户 J5@9s8w#cG9f(E
// Place holders and execute relatedPHPChina 开源社区门户/gFMK9d:i~
$db->quote();PHPChina 开源社区门户Q(\N7Dora3n
$db->prepare();PHPChina 开源社区门户#VP&X'mCf$k+T#b
$db->execute();
;L{\ Zl6SJK0
$db->executeMultiple();PHPChina 开源社区门户 s._4zZ rg9[,Ia2rz N
// TransactionsPHPChina 开源社区门户4Ef%r(s+x
$db->autoCommit();PHPChina 开源社区门户W1f2kwg0w1uzpVYD
$db->commit();PHPChina 开源社区门户 v"K-T]t*DS@LY
$db->rollback();PHPChina 开源社区门户/J/_)dm\
// Disconnection
/X pE;vg0
$db->disconnect();
G ~+X1\(qZfe7i0 
#[V9DaH0
/*
!y}8M3WH5S'Z0* From DB_result objects
$N(\t)B G%B0*/
V!_4s8]J!EO v'r v0// get the object with, ie:PHPChina 开源社区门户`RD'y6o6xm+g
$res $db->query('select * from foo');PHPChina 开源社区门户%]G V0k V#E,M%A
 
V g2YT)C K0
// Data fetch
4i:}I/iYio ELL0
$res->fetchRow();PHPChina 开源社区门户 C4F"]5F)xf;q|+P0K e
$res->fetchInto();
Del/B?,bz(S YG0
// Result Info
d2@pA _,Y0
$res->numCols();PHPChina 开源社区门户!rfny _
$res->numRows();
z Ic9E)F Fs.y0
$res->tableInfo();PHPChina 开源社区门户2E-Y(M&q;{N2T%^)K
// Free
}*A*dd:z]i0
$res->free();PHPChina 开源社区门户uC2H3g(}!ZFf
 PHPChina 开源社区门户B)n*U7H*E
/*
$i{WEqqg0* From DB_error objects
Uz2T8L+j/e2J0*/PHPChina 开源社区门户SF{AcYUR'sR
// get the object with, ie:PHPChina 开源社区门户1O/X6u|`+V'|
$error $db->query('select * from no_table');PHPChina 开源社区门户_UW!W"nM)QP7c
 
mwFoVv{0
$error->getMessage();PHPChina 开源社区门户-~Q3Yb,?H Yb(i~
$error->getDebugInfo();PHPChina 开源社区门户(t P'h\ND!n5q/SM
$error->toString();
6\:}\N5zJ0
?>
 

 

QOs-H6rI0

5.       错误处理机制

D[.o S3} N0

5.1.      Pear DB Error获得错误信息PHPChina 开源社区门户"of$HS+w

PHPChina 开源社区门户|w0Ph5d r5Y

所有从Pear DB返回的错误都是Pear Errors.这有一种方法来搜集:

4P,Y:C(o7z C3y%^0
 
<?php
7PBrw)I0
...PHPChina 开源社区门户{3OfMv
$res $db->query('select * from no_table');PHPChina 开源社区门户DB pmGn
if (
DB::isError($res)) {PHPChina 开源社区门户[X d L-pfo J
    
// get the portable error string
)w(^tn8}[0    
echo $res->getMessage();PHPChina 开源社区门户}3g.\(` \
}PHPChina 开源社区门户]+H&w.q rM8^e8N
?>
 

 

%^.|a&w2LJ.G+`T0

4.2          Debug Pear DB Errors

,K0X6S|%|0

"Ll3WS&Oz(jT*}V0Pear DB采用一种轻便的错误消息系统向用户报错。把错误信息简单翻译成其它语言或者对于一种特殊错误采取特殊的处理方式这都带来了很大的优点。但是对于开发人员来说这些提示并么有提供很有用的信息。想要得到真实的数据处理出错的信息,你可以使用getDebugInfo()方法:PHPChina 开源社区门户"D ]/k#^2I\"Y:Tf

 
<?phpPHPChina 开源社区门户'{P#QL;vv
$sql 
'select * from no_table';
7f8c@nU8L!B0if (
DB::isError($res $db->query($sql))) {PHPChina 开源社区门户t&k0}TP&e!h Y]
    
// get the native backend errorPHPChina 开源社区门户4JW^)B y+X8_b }6}
    // and the last queryPHPChina 开源社区门户cs8N-QC6^8x*@
    
echo $res->getDebugInfo();PHPChina 开源社区门户*x }@2p n:s-WC
}
"|$Dm$T1{ x0
?>
 

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

&I;N]$qJ0
 
<?php
/vBo7^)rb_+Ss'O0
// what messages to report
J&?1F?'O#n"Qh7e Aj0
error_reporting (E_ALL E_NOTICE);PHPChina 开源社区门户baBs*m"o9C
// this function will handle all reported errorsPHPChina 开源社区门户-o p$E0LO3u
function my_error_handler ($errno$errstr$errfile$errline) {
p4g.kY(VSj0    echo 
"In $errfile, line: $errline\n<br>$errstr";PHPChina 开源社区门户7]jG d4J/`-A/rdl
}PHPChina 开源社区门户$M9k0gb4|&a0~
set_error_handler ('my_error_handler');PHPChina 开源社区门户w6x/SIZ-H
$db DB::connect('pgsql://postgres@localhost/no_db');
RefDU)~ w%dI0...PHPChina 开源社区门户@T"lpzF
?>
 

 

iv3ib,T A?0

5.3  对错误采取自动处理PHPChina 开源社区门户4c0n3^r$Q)K6Nl

| H$TeVU(Yt([0      正如你所看见的,Pear DB提供了广泛的错误检测和报告机制,这强迫开发人员必需对返回的数据结果进行检查,是否有错。Pear DB同时照顾我们避免这种痛苦的工作,提供了一种灵活的体系,在一个错误出现的时候自动调用相应的措施。PHPChina 开源社区门户P#N^{.s ?GD

PHPChina 开源社区门户QJ _xye4y

这些可能的措施包括:

Ac"E`&s/do4i0
  • 返回错误对象(PEAR_ERROR_RETURN).这是默认的.
  • 打印错误(PEAR_ERROR_PRINT)
  • 打印错误信息并忽略执行(PEAR_ERROR_DIE)
  • PHP函数trigger_error()来列举错误(PEAR_ERROR_TRIGGER)
  • 把错误对象传递给一个函数或者类的方法(PEAR_ERROR_CALLBACK)
PHPChina 开源社区门户_~7H5V2W X

简单示例:

q5TGjS6\G"@0
 
<?phpPHPChina 开源社区门户9?#X8w#hJYeB
require_once 'DB.php';
\6^!t3zG%RbE4S0ta/~0
// Set the default action to take on errorPHPChina 开源社区门户 V5CPd@9Z(B.xeM
PEAR::setErrorHandling(PEAR_ERROR_DIE);PHPChina 开源社区门户&jX5C:L@D%ZJ-@dJP)O
// From here you don't need to check errors any more
z9P\s+\Q/yQw0
$db DB::connect('pgsql://postgres@localhost/my_database');PHPChina 开源社区门户K+L lj d1V
$res $db->query('select id from no_table');PHPChina 开源社区门户R/h$Y:hp+Id;S
// at this point the execution is aborted and the error message is raisen
B:n@i-I0
...PHPChina 开源社区门户2q9s\,f{+T/^ E
?>
 

TAG: 全是帅的不能再输啦

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar