php mysql数据库连接类程序代码

  1. lass cls_mysql{
  2. var $querynum = 0;
  3. var $link;
  4. var $histories;
  5. var $dbhost;
  6. var $dbuser;
  7. var $dbpw;
  8. var $dbcharset;
  9. var $pconnect;
  10. var $tablepre;
  11. var $time;
  12. var $goneaway = 5;
  13. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre='', $time = 0) {
  14. $this->dbhost = $dbhost;
  15. $this->dbuser = $dbuser;
  16. $this->dbpw = $dbpw;
  17. $this->dbname = $dbname;
  18. $this->dbcharset = $dbcharset;
  19. $this->pconnect = $pconnect;
  20. $this->tablepre = $tablepre;
  21. $this->time = $time;
  22. if($pconnect) {
  23. if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  24. $this->halt('can not connect to mysql server');
  25. }
  26. } else {
  27. if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw)) {
  28. $this->halt('can not connect to mysql server');
  29. }
  30. }
  31. if($this->version() > '4.1') {
  32. if($dbcharset) {
  33. mysql_query("set character_set_connection=".$dbcharset.", character_set_results=".$dbcharset.", character_set_client=binary", $this->link);
  34. }
  35. if($this->version() > '5.0.1') {
  36. mysql_query("set sql_mode=''", $this->link);
  37. }
  38. }
  39. if($dbname) {
  40. mysql_select_db($dbname, $this->link);
  41. }
  42. }
  43. function fetch_array($query, $result_type = mysql_assoc) {
  44. return mysql_fetch_array($query, $result_type);
  45. }
  46. function result_first($sql) {
  47. $query = $this->query($sql);
  48. return $this->result($query, 0);
  49. }
  50. function fetch_first($sql) {
  51. $query = $this->query($sql);
  52. return $this->fetch_array($query);
  53. }
  54. function fetch_all($sql, $id = '') {
  55. $arr = array();
  56. $query = $this->query($sql);
  57. while($data = $this->fetch_array($query)) {
  58. $id ? $arr[$data[$id]] = $data : $arr[] = $data;
  59. }
  60. return $arr;
  61. }
  62. function cache_gc() {
  63. $this->query("delete from {$this->tablepre}sqlcaches where expiry<$this->time");
  64. }
  65. function query($sql, $type = '', $cachetime = false) {
  66. $func = $type == 'unbuffered' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
  67. if(!($query = $func($sql, $this->link)) && $type != 'silent') {
  68. $this->halt('mysql query error', $sql);
  69. }
  70. $this->querynum++;
  71. $this->histories[] = $sql;
  72. return $query;
  73. }
  74. function affected_rows() {
  75. return mysql_affected_rows($this->link);
  76. }
  77. function error() {
  78. return (($this->link) ? mysql_error($this->link) : mysql_error());
  79. }
  80. function errno() {
  81. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  82. }
  83. function result($query, $row) {
  84. $query = @mysql_result($query, $row);
  85. return $query;
  86. }
  87. function num_rows($query) {
  88. $query = mysql_num_rows($query);
  89. return $query;
  90. }
  91. function num_fields($query) {
  92. return mysql_num_fields($query);
  93. }
  94. function free_result($query) {
  95. return mysql_free_result($query);
  96. }
  97. function insert_id() {
  98. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("select last_insert_id()"), 0);
  99. }
  100. function fetch_row($query) {
  101. $query = mysql_fetch_row($query);
  102. return $query;
  103. }
  104. function fetch_fields($query) {
  105. return mysql_fetch_field($query);
  106. }
  107. function version() {
  108. return mysql_get_server_info($this->link);
  109. }
  110. function close() {
  111. return mysql_close($this->link);
  112. }
  113. function halt($message = '', $sql = '') {
  114. $error = mysql_error();
  115. $errorno = mysql_errno();
  116. if($errorno == 2006 && $this->goneaway-- > 0) {
  117. $this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);//开源代码phpfensi.com
  118. $this->query($sql);
  119. } else {
  120. $s = '<b>error:</b>'.$error.'<br />';
  121. $s .= '<b>errno:</b>'.$errorno.'<br />';
  122. $s .= '<b>sql:</b>:'.$sql;
  123. exit($s);
  124. }
  125. }
  126. }