通過 Pear DB可以從查詢結(jié)果獲得更多有用的數(shù)據(jù)信息 。這些方法有: numRows(): 通過一個(gè)"SELECT" 查詢返回所有數(shù)據(jù)的數(shù)量。
<?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. 錯(cuò)誤處理機(jī)制
5.1. 從Pear DB Error獲得錯(cuò)誤信息
所有從Pear DB 返回的錯(cuò)誤都是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采用一種輕便的錯(cuò)誤消息系統(tǒng)向用戶報(bào)錯(cuò)。把錯(cuò)誤信息簡單翻譯成其它語言或者對于一種特殊錯(cuò)誤采取特殊的處理方式這都帶來了很大的優(yōu)點(diǎn)。但是對于開發(fā)人員來說這些提示并么有提供很有用的信息。想要得到真實(shí)的數(shù)據(jù)處理出錯(cuò)的信息,你可以使用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 (); } ?>
|
通過當(dāng)一個(gè)PHP函數(shù)出錯(cuò)時(shí),會(huì)打印出出錯(cuò)提示。在pear中的這種機(jī)制被屏蔽了。但時(shí)有時(shí)你可能需要在代碼中捕捉一些錯(cuò)誤信息??梢允褂?/FONT>set_error_handler PHP 函數(shù), 從 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 對錯(cuò)誤采取自動(dòng)處理
正如你所看見的, Pear DB提供了廣泛的錯(cuò)誤檢測和報(bào)告機(jī)制,這強(qiáng)迫開發(fā)人員必需對返回的數(shù)據(jù)結(jié)果進(jìn)行檢查,是否有錯(cuò)。 Pear DB同時(shí)照顧我們避免這種痛苦的工作,提供了一種靈活的體系,在一個(gè)錯(cuò)誤出現(xiàn)的時(shí)候自動(dòng)調(diào)用相應(yīng)的措施。
這些可能的措施包括:
- 返回錯(cuò)誤對象 (PEAR_ERROR_RETURN). 這是默認(rèn)的.
- 打印錯(cuò)誤 (PEAR_ERROR_PRINT)
- 打印錯(cuò)誤信息并忽略執(zhí)行(PEAR_ERROR_DIE)
- 用PHP函數(shù) trigger_error()來列舉錯(cuò)誤(PEAR_ERROR_TRIGGER)
- 把錯(cuò)誤對象傳遞給一個(gè)函數(shù)或者類的方法 (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 ()) { ... } ... ?>
|
下面為擴(kuò)展錯(cuò)誤機(jī)制提供了一個(gè)很好的想法:
<?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' ); ... ?>
|