最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pear DB 新手入門(mén)指南教程第3/3頁(yè)

 更新時(shí)間:2008年11月21日 19:01:51   作者:  
通過(guò) Pear DB可以從查詢(xún)結(jié)果獲得更多有用的數(shù)據(jù)信息 。這些方法有: numRows(): 通過(guò)一個(gè)"SELECT" 查詢(xún)返回所有數(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

 
<?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.        錯(cuò)誤處理機(jī)制

5.1.       從Pear DB Error獲得錯(cuò)誤信息

所有從Pear DB 返回的錯(cuò)誤都是Pear Errors. 這有一種方法來(lái)搜集:

 
<?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)向用戶(hù)報(bào)錯(cuò)。把錯(cuò)誤信息簡(jiǎn)單翻譯成其它語(yǔ)言或者對(duì)于一種特殊錯(cuò)誤采取特殊的處理方式這都帶來(lái)了很大的優(yōu)點(diǎn)。但是對(duì)于開(kāi)發(fā)人員來(lái)說(shuō)這些提示并么有提供很有用的信息。想要得到真實(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();
}
?>
 

通過(guò)當(dāng)一個(gè)PHP函數(shù)出錯(cuò)時(shí),會(huì)打印出出錯(cuò)提示。在pear中的這種機(jī)制被屏蔽了。但時(shí)有時(shí)你可能需要在代碼中捕捉一些錯(cuò)誤信息??梢允褂?/FONT>set_error_handler PHP 函數(shù), PHP Manual獲取信息.簡(jiǎn)單示例:

 
<?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   對(duì)錯(cuò)誤采取自動(dòng)處理

       正如你所看見(jiàn)的, Pear DB提供了廣泛的錯(cuò)誤檢測(cè)和報(bào)告機(jī)制,這強(qiáng)迫開(kāi)發(fā)人員必需對(duì)返回的數(shù)據(jù)結(jié)果進(jìn)行檢查,是否有錯(cuò)。 Pear DB同時(shí)照顧我們避免這種痛苦的工作,提供了一種靈活的體系,在一個(gè)錯(cuò)誤出現(xiàn)的時(shí)候自動(dòng)調(diào)用相應(yīng)的措施。

這些可能的措施包括:

  • 返回錯(cuò)誤對(duì)象 (PEAR_ERROR_RETURN). 這是默認(rèn)的.
  • 打印錯(cuò)誤 (PEAR_ERROR_PRINT)
  • 打印錯(cuò)誤信息并忽略執(zhí)行(PEAR_ERROR_DIE)
  • 用PHP函數(shù) trigger_error()來(lái)列舉錯(cuò)誤(PEAR_ERROR_TRIGGER)
  • 把錯(cuò)誤對(duì)象傳遞給一個(gè)函數(shù)或者類(lèi)的方法 (PEAR_ERROR_CALLBACK)

簡(jiǎn)單示例:

 
<?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
...
?>
 

高級(jí)示例:

 
<?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_stringE_USER_ERROR);
}
require 'DB.php';
PEAR::setErrorHandling (PEAR_ERROR_CALLBACK'pear_error_handler');
// force an error
$db DB::connect('pgsql://postgres@localhost/no_db');
...
?>

相關(guān)文章

最新評(píng)論

西贡区| 万全县| 松潘县| 韩城市| 蛟河市| 铅山县| 巧家县| 塔河县| 兴化市| 连城县| 武汉市| 法库县| 互助| 清流县| 周口市| 四川省| 县级市| 莲花县| 南陵县| 深泽县| 海伦市| 梁河县| 比如县| 高密市| 民县| 东光县| 九江县| 武夷山市| 明星| 来凤县| 安国市| 富裕县| 遂川县| 嵩明县| 广汉市| 南投县| 曲麻莱县| 金平| 天门市| 临高县| 洪雅县|