access.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. defined('IN_PHPCMS') or exit('Access Denied');
  3. class access{
  4. var $querynum = 0;
  5. var $conn;
  6. var $insertid = 0;
  7. var $cursor = 0;
  8. //var $ADODB_FETCH_MODE = ADODB_FETCH_BOTH;
  9. /**
  10. * 最近一次查询资源句柄
  11. */
  12. public $lastqueryid = null;
  13. function connect($dbhost, $dbuser = '', $dbpw = '', $dbname = '', $pconnect = 0)
  14. {
  15. $this->conn = new com('adodb.connection');
  16. if(!$this->conn) return false;
  17. $this->conn->open("DRIVER={Microsoft Access Driver (*.mdb)};dbq=$dbhost;uid=$dbuser;pwd=$dbpw");
  18. if($this->conn->state == 0)
  19. {
  20. $this->conn->open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dbhost");
  21. if($this->conn->state == 0) return false;;
  22. }
  23. define('NUM', 1);
  24. define('ASSOC', 2);
  25. define('BOTH', 3);
  26. return $this->conn->state;
  27. }
  28. function select_db($dbname)
  29. {
  30. return $this->conn->state;
  31. }
  32. function query($sql, $type = '', $expires = 3600, $dbname = '') {
  33. $this->querynum++;
  34. $sql = trim($sql);
  35. if(preg_match("/^(select.*)limit ([0-9]+)(,([0-9]+))?$/i", $sql, $matchs)){
  36. $sql = $matchs[1];
  37. $offset = $matchs[2];
  38. $pagesize = $matchs[4];
  39. $query = $this->conn->Execute($sql);
  40. return $this->limit($query, $offset, $pagesize);
  41. } else{
  42. return $this->conn->Execute($sql);
  43. }
  44. }
  45. function get_one($query) {
  46. $this->querynum++;
  47. $rs = $this->conn->Execute($query);
  48. $r = $this->fetch_array($rs);
  49. $this->free_result($rs);
  50. return $r;
  51. }
  52. function fetch_array($rs, $result_type = 3) {
  53. if(is_array($rs)){
  54. return $this->cursor < count($rs) ? $rs[$this->cursor++] : FALSE;
  55. } else{
  56. if($rs->EOF) return FALSE;
  57. $array = array();
  58. for($i = 0; $i < $this->num_fields($rs); $i++){
  59. $fielddata = $rs->Fields[$i]->Value;
  60. $array[$rs->Fields[$i]->Name] = $fielddata;
  61. }
  62. $rs->MoveNext();
  63. return $array;
  64. }
  65. }
  66. function select($sql, $keyfield = ''){
  67. $array = array();
  68. $result = $this->query($sql);
  69. while($r = $this->fetch_array($result)){
  70. if($keyfield){
  71. $key = $r[$keyfield];
  72. $array[$key] = $r;
  73. }else{
  74. $array[] = $r;
  75. }
  76. }
  77. $this->free_result($result);
  78. return $array;
  79. }
  80. function num_rows($rs){
  81. return is_array($rs) ? count($rs) : $rs->recordcount;
  82. }
  83. function num_fields($rs){
  84. return $rs->Fields->Count;
  85. }
  86. function fetch_assoc($rs){
  87. return $this->fetch_array($rs, ASSOC);
  88. }
  89. function fetch_row($rs){
  90. return $this->fetch_array($rs, NUM);
  91. }
  92. function free_result($rs){
  93. if(is_resource($rs)) $rs->close();
  94. }
  95. function error(){
  96. return $this->conn->Errors[$this->conn->Errors->Count-1]->Number;
  97. }
  98. function errormsg(){
  99. return $this->conn->Errors[$this->conn->Errors->Count-1]->Description;
  100. }
  101. function close(){
  102. $this->conn->close();
  103. }
  104. function limit($rs, $offset, $pagesize = 0){
  105. if($pagesize > 0){
  106. $rs->Move($offset);
  107. }else{
  108. $pagesize = $offset;
  109. }
  110. $info = array();
  111. for($i = 0; $i < $pagesize; $i++){
  112. $r = $this->fetch_array($rs);
  113. if(!$r) break;
  114. $info[] = $r;
  115. }
  116. $this->free_result($rs);
  117. $this->cursor = 0;
  118. return $info;
  119. }
  120. }
  121. ?>