PEAR::DB的二次封装

来源:互联网 发布:数据挖掘 实战视频 编辑:程序博客网 时间:2024/05/17 23:27
无标题文档

PEAR::DB的二次封装

本贴转载自:http://blog.csdn.net/dfmz007/archive/2004/07/29/55911.aspx

1. ? 2.
? 3. require_once("DB.php");
? 4.
? 5. //
? 6. // 简单够用的DB封装库,继承自PEAR::DB
? 7. // 连接参数$dsn由类构造时传入
? 8. // 使用方法
? 9. // 1 声明类的实体 $db = new PEAR_DB("数据库类型://用户名:用户口令@服务器地址/使用的数据库名称");
? 10. // 2 输入SQL查询 $db->query($SQL);
? 11. // 3 取出结果集中的一行数据 $db->fetchRow();
? 12. // 4 直接取数据使用 $db->record['var_name']; (若执行的SQL语句无返回结果集,3-4 两步可跳过,比如 INSERT ? UPDATE DELETE )
? 13. // 5 关闭数据库连接 $db->disconnect();
? 14. //
? 15. // Build 20040625
? 16. //
? 17. class PEAR_DB extends DB
? 18. {
? 19.
? 20. var $pConnect = false; // 持续连接开关
? 21. var $AutoFree = true; // 自动释放结果集开关
? 22. var $HaltOnErr = true; // 出错挂起开关
? 23. var $AdminMail = ""; // 管理员邮箱
? 24. var $DEBUG = true; // 调试开关
? 25. var $Version = "dxDB Build 20040625"; // 版本号记录
? 26.
? 27. var $dsn = "";
? 28. var $obj = null;
? 29. var $result = null;
? 30. var $record = null;
? 31.
? 32. var $QueryNo = 0; // 查询次数,调试时使用
? 33. var $ErrNo = 0; // 错误号记录
? 34. var $ErrMsg = ""; // 出错信息记录
? 35.
? 36. function PEAR_DB($dsn = "mysql://root:root@localhost/test")
? 37. {
? 38. $this->dsn = $dsn;
? 39. $options = array(
? 40. 'debug' => 2,
? 41. 'portability' => DB_PORTABILITY_ALL,
? 42. 'persistent' => $this->pConnect,
? 43. );
? 44. $this->obj = DB::connect($this->dsn, $options);
? 45. if(DB::isError($this->obj)){
? 46. $this->halt();
? 47. }
? 48. $this->obj->setFetchMode(DB_FETCHMODE_ASSOC);
? 49. }
? 50.
? 51. //
? 52. // 提交SQL查询,返回执行后的结果集
? 53. // 根据 $this->DEBUG 开关打印查询语句以及执行时间
? 54. //
? 55. function query($SQL, $offsetORcount = -1, $num = -1)
? 56. {
? 57. $this->QueryNo ++;
? 58. if($this->result != NULL && $this->AutoFree) $this->result->free();
? 59. if(!empty($SQL)){
? 60. $QueryTime_Start = microtime();
? 61. $SQL = str_replace(";", " ", $SQL);
? 62. if($offsetORcount != -1){
? 63. if($num == -1){
? 64. //使用形如 LIMIT n 的格式,只查询前n条记录
? 65. $SQL .= " Limit {$offsetORcount}";
? 66. }
? 67. else{
? 68. //使用完整的 LIMIT m, n 格式,查询第m条起n条记录
? 69. $SQL .= " Limit {$offsetORcount}, {$num}";
? 70. }
? 71. }
? 72. $this->result = $this->obj->query($SQL);
? 73. $QueryTime_End = microtime();
? 74. if($this->DEBUG){
? 75. //若有Debug参数,打印出SQL的相关信息。
? 76. list($ST_mSec, $ST_Sec) = explode(' ', $QueryTime_Start);
? 77. list($ET_mSec, $ET_Sec) = explode(' ', $QueryTime_End);
? 78. $ExeTime = round((($ET_Sec - $ST_Sec) + ($ET_mSec - $ST_mSec)), 6);
? 79. print("Query:{$this->QueryNo} ExecuteTime:{$ExeTime}(S) SQL:{$SQL}
");
? 80. }
? 81. if(DB::isError($this->result)){
? 82. $this->halt();
? 83. }
? 84. }
? 85. return $this->result;
? 86. }
? 87.
? 88. function fetchRow()
? 89. {
? 90. $this->record = $this->result->fetchRow();
? 91. return $this->record;
? 92. }
? 93.
? 94. function numRows()
? 95. {
? 96. return $this->result->numRows();
? 97. }
? 98.
? 99. function free()
? 100. {
? 101. if($this->result != null) $this->result->free();
? 102. }
? 103.
? 104. function disconnect()
? 105. {
? 106. $this->free();
? 107. return $this->obj->disconnect();
? 108. }
? 109.
? 110. //
? 111. // 记录可能产生的错误
? 112. //
? 113. function logErr()
? 114. {
? 115. $this->ErrNo = $this->obj->errorNative();
? 116. $this->ErrMsg = $this->result->getMessage();
? 117. }
? 118.
? 119. //
? 120. // 数据库出错后的处理函数
? 121. // 根据$this->Con_HaltOnErr开关决定是否显示错误报告并挂起程序
? 122. //
? 123. function halt($Msg = "")
? 124. {
? 125. if($this->HaltOnErr){
? 126. ob_end_clean();
? 127. $Time = date('Y-m-d H:i:s');
? 128. $Error = $this->result->getMessage();
? 129. $Errno = $this->obj->errorNative();
? 130. $Str = "
? 131.
? 132.
? 133.
? 134. Database Error
? 135.
? 136. P, TEXTAREA, BODY { FONT-FAMILY:Verdana, Arial, Helvetica; ? FONT-SIZE:9pt; }
? 137.
? 138.


? 139.

 


? 140.

数据库好象发生了一些微小的错误.


? 141. 请按浏览器的 刷新 ? 按钮重试


? 142. 或者联系 AdminMail}'>技术支持信箱

? 143.

给您带来不便与困扰,我们着实深感抱歉...


? 144.
? 154.

? 155.
? 156.
? 157. ";
? 158. print($Str);
? 159. exit();
? 160. }
? 161. else{
? 162. $this->logErr();
? 163. }
? 164. $this->Free();
? 165. }
? 166.
? 167. function version()
? 168. {
? 169. return $this->Version;
? 170. }
? 171.
? 172. }
? 173.
? 174. ?>

posted on 2004年07月29日 10:53 PM
?

??? ??? ???
原创粉丝点击