PEAR::DB的二次封装

  1. <?
  2. require_once("DB.php");
    • //
  3. //    简单够用的DB封装库,继承自PEAR::DB
  4. //    连接参数$dsn由类构造时传入
  5. //    使用方法
  6. //    1 声明类的实体           $db = new PEAR_DB("数据库类型://用户名:用户口令@服务器地址/使用的数据库名称");
  7. //    2 输入SQL查询            $db->query($SQL);
  8. //    3 取出结果集中的一行数据 $db->fetchRow();
  9. //    4 直接取数据使用         $db->record['var_name'];    (若执行的SQL语句无返回结果集,3-4 两步可跳过,比如 INSERT UPDATE DELETE )
  10. //    5 关闭数据库连接         $db->disconnect();
  11. //
  12. //    Build 20040625
  13. //
  14. class PEAR_DB extends DB
  15. {
  16.     var $pConnect  = false;                  // 持续连接开关
  17.     var $AutoFree  = true;                   // 自动释放结果集开关
  18.     var $HaltOnErr = true;                   // 出错挂起开关
  19.     var $AdminMail = "";                     // 管理员邮箱
  20.     var $DEBUG     = true;                   // 调试开关
  21.     var $Version   = "dxDB Build 20040625";  // 版本号记录
  22.     var $dsn = "";
  23.     var $obj = null;
  24.     var $result = null;
  25.     var $record = null;
  26.     var $QueryNo = 0;                        // 查询次数,调试时使用
  27.     var $ErrNo   = 0;                        // 错误号记录
  28.     var $ErrMsg  = "";                       // 出错信息记录
  29.     function PEAR_DB($dsn = "mysql://root:root@localhost/test")
  30.     {
  31.  $this->dsn = $dsn;
  32.  $options = array(
  33.      'debug'       => 2,
  34.      'portability' => DB_PORTABILITY_ALL,
  35.      'persistent'  => $this->pConnect,
  36.  );
  37.  $this->obj = DB::connect($this->dsn, $options);
  38.  if(DB::isError($this->obj)){
  39.      $this->halt();
  40.  }
  41.  $this->obj->setFetchMode(DB_FETCHMODE_ASSOC);
  42.     }
  43.     //
  44.     //    提交SQL查询,返回执行后的结果集
  45.     //    根据 $this->DEBUG 开关打印查询语句以及执行时间
  46.     //
  47.     function query($SQL, $offsetORcount = -1, $num = -1)
  48.     {
  49.  $this->QueryNo ++;
  50.  if($this->result != NULL && $this->AutoFree) $this->result->free();
  51.  if(!empty($SQL)){
  52.      $QueryTime_Start = microtime();
  53.      $SQL = str_replace(";", " ", $SQL);
  54.      if($offsetORcount != -1){
  55.    if($num == -1){
  56.        //使用形如 LIMIT n 的格式,只查询前n条记录
  57.        $SQL .= " Limit {$offsetORcount}";
  58.    }
  59.    else{
  60.        //使用完整的 LIMIT m, n 格式,查询第m条起n条记录
  61.        $SQL .= " Limit {$offsetORcount}, {$num}";
  62.    }
  63.      }
  64.      $this->result = $this->obj->query($SQL);
  65.      $QueryTime_End = microtime();
  66.      if($this->DEBUG){
  67.    //若有Debug参数,打印出SQL的相关信息。
  68.    list($ST_mSec, $ST_Sec) = explode(' ', $QueryTime_Start);
  69.    list($ET_mSec, $ET_Sec) = explode(' ', $QueryTime_End);
  70.    $ExeTime = round((($ET_Sec - $ST_Sec) + ($ET_mSec - $ST_mSec)), 6);
  71.    print("Query:{$this->QueryNo} ExecuteTime:{$ExeTime}(S) SQL:{$SQL}<BR>");
  72.      }
  73.      if(DB::isError($this->result)){
  74.    $this->halt();
  75.      }
  76.  }
  77.  return $this->result;
  78.     }
  79.     function fetchRow()
  80.     {
  81.  $this->record = $this->result->fetchRow();
  82.  return $this->record;
  83.     }
  84.     function numRows()
  85.     {
  86.  return $this->result->numRows();
  87.     }
  88.     function free()
  89.     {
  90.  if($this->result != null) $this->result->free();
  91.     }
  92.     function disconnect()
  93.     {
  94.  $this->free();
  95.  return $this->obj->disconnect();
  96.     }
  97.     //
  98.     //    记录可能产生的错误
  99.     //
  100.     function logErr()
  101.     {
  102.  $this->ErrNo  = $this->obj->errorNative();
  103.  $this->ErrMsg = $this->result->getMessage();
  104.     }
  105.     //
  106.     //    数据库出错后的处理函数
  107.     //    根据$this->Con_HaltOnErr开关决定是否显示错误报告并挂起程序
  108.     //
  109.     function halt($Msg = "")
  110.     {
  111.  if($this->HaltOnErr){
  112.      ob_end_clean();
  113.      $Time  = date('Y-m-d H:i:s');
  114.      $Error = $this->result->getMessage();
  115.      $Errno = $this->obj->errorNative();
  116.      $Str = "
  117. <html>
  118. <head>
  119. <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
  120. <title>Database Error</title>
  121. </head>
  122. <style> P, TEXTAREA, BODY { FONT-FAMILY:Verdana, Arial, Helvetica; FONT-SIZE:9pt; } </style>
  123. <body>
  124. <blockquote>
  125. <p>&nbsp;</p>
  126. <p><strong>数据库好象发生了一些微小的错误.</strong><br><br>
  127. 请按浏览器的 <a href='javascript:window.location=window.location;'>刷新</a> 按钮重试</p>
  128. 或者联系 <a href='mailto:{$this->AdminMail}'>技术支持信箱</a><br>
  129. <p>给您带来不便与困扰,我们着实深感抱歉...</p>
  130. <textarea rows='12' cols='60'>数据库错误报告来自于 {$this->Version}
  131. 错误信息:{$Error}
  132. 错误号码:{$Errno}
  133. 时间:{$Time}
  134. 页面:{$_SERVER['PHP_SELF']}
  135. 附注:{$Msg}
  136. </textarea>
  137. </blockquote>
  138. </body>
  139. </html>
  140. ";
  141.      print($Str);
  142.      exit();
  143.  }
  144.  else{
  145.      $this->logErr();
  146.  }
  147.  $this->Free();
  148.     }
  149.     function version()
  150.     {
  151.  return $this->Version;
  152.     }
  153. }
  154. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值