db_access.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * access.class.php 数据库实现类
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-6-1
  8. */
  9. final class db_access {
  10. /**
  11. * 数据库配置信息
  12. */
  13. private $config = null;
  14. /**
  15. * 数据库连接资源句柄
  16. */
  17. public $link = null;
  18. /**
  19. * 最近一次查询资源句柄
  20. */
  21. public $lastqueryid = null;
  22. /**
  23. * 统计数据库查询次数
  24. */
  25. public $querycount = 0;
  26. public function __construct() {
  27. }
  28. /**
  29. * 打开数据库连接,有可能不真实连接数据库
  30. * @param $config 数据库连接参数
  31. *
  32. * @return void
  33. */
  34. public function open($config) {
  35. $this->config = $config;
  36. if($config['autoconnect'] == 1) {
  37. $this->connect();
  38. }
  39. }
  40. /**
  41. * 真正开启数据库连接
  42. *
  43. * @return void
  44. */
  45. public function connect() {
  46. /*真正的连接程序*/
  47. $dbhost = $this->config['hostname'];
  48. $dbuser = $this->config['username'];
  49. $dbpwd = $this->config['password'];
  50. $this->conn = new com('adodb.connection');
  51. if(!$this->conn) return false;
  52. $this->conn->open("DRIVER={Microsoft Access Driver (*.mdb)};dbq=$dbhost;uid=$dbuser;pwd=$dbpw");
  53. if($this->conn->state == 0){
  54. $this->conn->open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dbhost");
  55. if($this->conn->state == 0) return false;;
  56. }
  57. define('NUM', 1);
  58. define('ASSOC', 2);
  59. define('BOTH', 3);
  60. return $this->conn->state;
  61. }
  62. function select_db($dbname)
  63. {
  64. return $this->conn->state;
  65. }
  66. function query($sql, $type = '', $expires = 3600, $dbname = '')
  67. {
  68. $this->querynum++;
  69. $sql = trim($sql);
  70. if(preg_match("/^(select.*)limit ([0-9]+)(,([0-9]+))?$/i", $sql, $matchs))
  71. {
  72. $sql = $matchs[1];
  73. $offset = $matchs[2];
  74. $pagesize = $matchs[4];
  75. $query = $this->conn->Execute($sql);
  76. return $this->limit($query, $offset, $pagesize);
  77. }
  78. else
  79. {
  80. return $this->conn->Execute($sql);
  81. }
  82. }
  83. function get_one($query)
  84. {
  85. $this->querynum++;
  86. $rs = $this->conn->Execute($query);
  87. $r = $this->fetch_array($rs);
  88. $this->free_result($rs);
  89. return $r;
  90. }
  91. function fetch_array($rs, $result_type = 3)
  92. {
  93. if(is_array($rs))
  94. {
  95. return $this->cursor < count($rs) ? $rs[$this->cursor++] : FALSE;
  96. }
  97. else
  98. {
  99. if($rs->EOF) return FALSE;
  100. $array = array();
  101. for($i = 0; $i < $this->num_fields($rs); $i++)
  102. {
  103. $fielddata = $rs->Fields[$i]->Value;
  104. if($result_type == NUM || $result_type == BOTH) $array[$i] = $fielddata;
  105. if($result_type == ASSOC || $result_type == BOTH) $array[$rs->Fields[$i]->Name] = $fielddata;
  106. }
  107. $rs->MoveNext();
  108. return $array;
  109. }
  110. }
  111. function select($sql, $keyfield = '')
  112. {
  113. $array = array();
  114. $result = $this->query($sql);
  115. while($r = $this->fetch_array($result))
  116. {
  117. if($keyfield)
  118. {
  119. $key = $r[$keyfield];
  120. $array[$key] = $r;
  121. }
  122. else
  123. {
  124. $array[] = $r;
  125. }
  126. }
  127. $this->free_result($result);
  128. return $array;
  129. }
  130. function num_rows($rs)
  131. {
  132. return is_array($rs) ? count($rs) : $rs->recordcount;
  133. }
  134. function num_fields($rs)
  135. {
  136. return $rs->Fields->Count;
  137. }
  138. function fetch_assoc($rs)
  139. {
  140. return $this->fetch_array($rs, ASSOC);
  141. }
  142. function fetch_row($rs)
  143. {
  144. return $this->fetch_array($rs, NUM);
  145. }
  146. function free_result($rs)
  147. {
  148. if(is_resource($rs)) $rs->close();
  149. }
  150. function error()
  151. {
  152. return $this->conn->Errors[$this->conn->Errors->Count-1]->Number;
  153. }
  154. function errormsg()
  155. {
  156. return $this->conn->Errors[$this->conn->Errors->Count-1]->Description;
  157. }
  158. function close()
  159. {
  160. $this->conn->close();
  161. }
  162. function limit($rs, $offset, $pagesize = 0)
  163. {
  164. if($pagesize > 0)
  165. {
  166. $rs->Move($offset);
  167. }
  168. else
  169. {
  170. $pagesize = $offset;
  171. }
  172. $info = array();
  173. for($i = 0; $i < $pagesize; $i++)
  174. {
  175. $r = $this->fetch_array($rs);
  176. if(!$r) break;
  177. $info[] = $r;
  178. }
  179. $this->free_result($rs);
  180. $this->cursor = 0;
  181. return $info;
  182. }
  183. }
  184. ?>