mysql 数据库操作类

本款是利用php mysql 数据库操作类函数,可以删除,修改,增加,备份等对数据库的操作,同时还支持两种数据库连接方式,以前数据库编码等的设置,代码如下:

  1. class dbstuff {
  2. var $querynum = 0;
  3. var $link;
  4. var $sqlid;
  5. var $record;
  6. var $dbcharset='utf-8';
  7. var $charset='utf-8';
  8. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = true) {
  9. if($pconnect) {
  10. if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  11. $halt && $this->halt('can not connect to mysql server');
  12. }
  13. } else {
  14. if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
  15. $halt && $this->halt('can not connect to mysql server');
  16. }
  17. }
  18. if($this->version() > '4.1') {
  19. global $charset, $dbcharset;
  20. if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
  21. $dbcharset = str_replace('-', '', $charset);
  22. }
  23. if($dbcharset) {
  24. @mysql_query("set character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
  25. }
  26. if($this->version() > '5.0.1') {
  27. @mysql_query("set sql_mode=''", $this->link);
  28. }
  29. }
  30. if($dbname) {
  31. @mysql_select_db($dbname, $this->link);
  32. }
  33. }
  34. function select_db($dbname) {
  35. return mysql_select_db($dbname, $this->link);
  36. }
  37. function fetch_array($query, $result_type = mysql_assoc) {
  38. return mysql_fetch_array($query, $result_type);
  39. }
  40. function query($sql, $type = '') {
  41. global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes;
  42. $func = $type == 'unbuffered' && @function_exists('mysql_unbuffered_query') ?
  43. 'mysql_unbuffered_query' : 'mysql_query';
  44. if(!($query = $func($sql, $this->link))) {
  45. if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'retry') {
  46. $this->close();
  47. $this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
  48. $this->query($sql, 'retry'.$type);
  49. } elseif($type != 'silent' && substr($type, 5) != 'silent') {
  50. $this->halt('mysql query error', $sql);
  51. }
  52. }
  53. $this->querynum++;
  54. $this->sqlid=$query;
  55. return $query;
  56. }
  57. function affected_rows() {
  58. return mysql_affected_rows($this->link);
  59. }
  60. function error() {
  61. return (($this->link) ? mysql_error($this->link) : mysql_error());
  62. }
  63. function errno() {
  64. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  65. }
  66. function result($query, $row) {
  67. $query = @mysql_result($query, $row);
  68. return $query;
  69. }
  70. function result_first($sql) {
  71. return $this->result($this->query($sql), 0);
  72. }
  73. function num_rows($query) {
  74. $query = mysql_num_rows($query);
  75. return $query;
  76. }
  77. function num_fields($query) {
  78. return mysql_num_fields($query);
  79. }
  80. function free_result($query) {
  81. return mysql_free_result($query);
  82. }
  83. function insert_id() {
  84. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("select last_insert_id()"), 0);
  85. }
  86. function fetch_row($query) {
  87. $query = mysql_fetch_row($query);
  88. return $query;
  89. }
  90. function fetch_fields($query) {
  91. return mysql_fetch_field($query);
  92. }
  93. function version() {
  94. return mysql_get_server_info($this->link);
  95. }
  96. function close() {
  97. return mysql_close($this->link);
  98. }
  99. function nr($sql_) {
  100. if(!$sql_id) $sql_id=$this->sqlid;
  101. return mysql_num_rows($sql_id);
  102. }
  103. function nf($sql_) {
  104. if(!$sql_id) $sql_id=$this->sqlid;
  105. return mysql_num_fields($sql_id);
  106. }
  107. function nextrecord($sql_) {
  108. if(!$sql_id) $sql_id=$this->sqlid;
  109. if($this->record=mysql_fetch_array($sql_id)) return $this->record;
  110. else return false;
  111. }
  112. function f($name) {
  113. if($this->record[$name]) return $this->record[$name];
  114. else return false;
  115. }
  116. function lock($tblname,$op="write") {
  117. if(mysql_query("lock tables ".$tblname." ".$op)) return true;
  118. else return false;
  119. }
  120. function unlock()
  121. {if(mysql_query("unlock tables")) return true; else return false;}
  122. function ar() {
  123. return @mysql_affected_rows($this->link);
  124. }
  125. function i_id() {
  126. return mysql_insert_id();
  127. }
  128. function halt($message = '', $sql = '') {
  129. define('cache_forbidden', true);
  130. include('db_mysql_error.inc.php');
  131. }//开源代码phpfensi.com
  132. }