- <?
- require_once("DB.php");
-
- //
- // 简单够用的DB封装库,继承自PEAR::DB
- // 连接参数$dsn由类构造时传入
- // 使用方法
- // 1 声明类的实体 $db = new PEAR_DB("数据库类型://用户名:用户口令@服务器地址/使用的数据库名称");
- // 2 输入SQL查询 $db->query($SQL);
- // 3 取出结果集中的一行数据 $db->fetchRow();
- // 4 直接取数据使用 $db->record['var_name']; (若执行的SQL语句无返回结果集,3-4 两步可跳过,比如 INSERT UPDATE DELETE )
- // 5 关闭数据库连接 $db->disconnect();
- //
- // Build 20040625
- //
- class PEAR_DB extends DB
- {
- var $pConnect = false; // 持续连接开关
- var $AutoFree = true; // 自动释放结果集开关
- var $HaltOnErr = true; // 出错挂起开关
- var $AdminMail = ""; // 管理员邮箱
- var $DEBUG = true; // 调试开关
- var $Version = "dxDB Build 20040625"; // 版本号记录
- var $dsn = "";
- var $obj = null;
- var $result = null;
- var $record = null;
- var $QueryNo = 0; // 查询次数,调试时使用
- var $ErrNo = 0; // 错误号记录
- var $ErrMsg = ""; // 出错信息记录
- function PEAR_DB($dsn = "mysql://root:root@localhost/test")
- {
- $this->dsn = $dsn;
- $options = array(
- 'debug' => 2,
- 'portability' => DB_PORTABILITY_ALL,
- 'persistent' => $this->pConnect,
- );
- $this->obj = DB::connect($this->dsn, $options);
- if(DB::isError($this->obj)){
- $this->halt();
- }
- $this->obj->setFetchMode(DB_FETCHMODE_ASSOC);
- }
- //
- // 提交SQL查询,返回执行后的结果集
- // 根据 $this->DEBUG 开关打印查询语句以及执行时间
- //
- function query($SQL, $offsetORcount = -1, $num = -1)
- {
- $this->QueryNo ++;
- if($this->result != NULL && $this->AutoFree) $this->result->free();
- if(!empty($SQL)){
- $QueryTime_Start = microtime();
- $SQL = str_replace(";", " ", $SQL);
- if($offsetORcount != -1){
- if($num == -1){
- //使用形如 LIMIT n 的格式,只查询前n条记录
- $SQL .= " Limit {$offsetORcount}";
- }
- else{
- //使用完整的 LIMIT m, n 格式,查询第m条起n条记录
- $SQL .= " Limit {$offsetORcount}, {$num}";
- }
- }
- $this->result = $this->obj->query($SQL);
- $QueryTime_End = microtime();
- if($this->DEBUG){
- //若有Debug参数,打印出SQL的相关信息。
- list($ST_mSec, $ST_Sec) = explode(' ', $QueryTime_Start);
- list($ET_mSec, $ET_Sec) = explode(' ', $QueryTime_End);
- $ExeTime = round((($ET_Sec - $ST_Sec) + ($ET_mSec - $ST_mSec)), 6);
- print("Query:{$this->QueryNo} ExecuteTime:{$ExeTime}(S) SQL:{$SQL}<BR>");
- }
- if(DB::isError($this->result)){
- $this->halt();
- }
- }
- return $this->result;
- }
- function fetchRow()
- {
- $this->record = $this->result->fetchRow();
- return $this->record;
- }
- function numRows()
- {
- return $this->result->numRows();
- }
- function free()
- {
- if($this->result != null) $this->result->free();
- }
- function disconnect()
- {
- $this->free();
- return $this->obj->disconnect();
- }
- //
- // 记录可能产生的错误
- //
- function logErr()
- {
- $this->ErrNo = $this->obj->errorNative();
- $this->ErrMsg = $this->result->getMessage();
- }
- //
- // 数据库出错后的处理函数
- // 根据$this->Con_HaltOnErr开关决定是否显示错误报告并挂起程序
- //
- function halt($Msg = "")
- {
- if($this->HaltOnErr){
- ob_end_clean();
- $Time = date('Y-m-d H:i:s');
- $Error = $this->result->getMessage();
- $Errno = $this->obj->errorNative();
- $Str = "
- <html>
- <head>
- <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
- <title>Database Error</title>
- </head>
- <style> P, TEXTAREA, BODY { FONT-FAMILY:Verdana, Arial, Helvetica; FONT-SIZE:9pt; } </style>
- <body>
- <blockquote>
- <p> </p>
- <p><strong>数据库好象发生了一些微小的错误.</strong><br><br>
- 请按浏览器的 <a href='javascript:window.location=window.location;'>刷新</a> 按钮重试</p>
- 或者联系 <a href='mailto:{$this->AdminMail}'>技术支持信箱</a><br>
- <p>给您带来不便与困扰,我们着实深感抱歉...</p>
- <textarea rows='12' cols='60'>数据库错误报告来自于 {$this->Version}
- 错误信息:{$Error}
- 错误号码:{$Errno}
- 时间:{$Time}
- 页面:{$_SERVER['PHP_SELF']}
- 附注:{$Msg}
- </textarea>
- </blockquote>
- </body>
- </html>
- ";
- print($Str);
- exit();
- }
- else{
- $this->logErr();
- }
- $this->Free();
- }
- function version()
- {
- return $this->Version;
- }
- }
- ?>