mysql数据库连接程序

这里提供的数据库连接类程序,后面还提供了一个sql安全检测函数与sql语句完整性检测函数,实例代码如下:

  1. class db_mysql {
  2. var $connid;
  3. var $querynum = 0;
  4. var $expires;
  5. var $cursor = 0;
  6. var $cache_id = '';
  7. var $cache_file = '';
  8. var $cache_expires = '';
  9. var $halt = 0;
  10. var $result = array();
  11. function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
  12. global $cfg;
  13. $this->expires = $cfg['db_expires'];
  14. $func = $pconnect == 1 ? 'mysql_pconnect' : 'mysql_connect';
  15. if(!$this->connid = $func($dbhost, $dbuser, $dbpw)) {
  16. $this->halt('can not connect to mysql server');
  17. }
  18. if($this->version() > '4.1' && $cfg['db_charset']) {
  19. mysql_query("set names '".$cfg['db_charset']."'" , $this->connid);
  20. }
  21. if($this->version() > '5.0') {
  22. mysql_query("set sql_mode=''" , $this->connid);
  23. }
  24. if($dbname) {
  25. if(!mysql_select_db($dbname , $this->connid)) {
  26. $this->halt('cannot use database '.$dbname);
  27. }
  28. }
  29. return $this->connid;
  30. }
  31. function select_db($dbname) {
  32. return mysql_select_db($dbname , $this->connid);
  33. }
  34. function query($sql , $type = '', $expires = 0, $save_id = false) {
  35. $sql=checksql($sql);
  36. if($type == 'cache' && stristr($sql, 'select')) {
  37. $this->cursor = 0;
  38. $this->cache_id = md5($sql);
  39. $this->result = array();
  40. $this->cache_expires = $expires ? $expires + mt_rand(-9, 9) : $this->expires;
  41. return $this->_query($sql);
  42. }
  43. if(!$save_id) $this->cache_id = 0;
  44. $func = $type == 'unbuffered' ? 'mysql_unbuffered_query' : 'mysql_query';
  45. if(!($query = $func($sql , $this->connid)) && $this->halt) {
  46. $this->halt('mysql query error', $sql);
  47. }
  48. $this->querynum++;
  49. return $query;
  50. }
  51. function get_one($sql, $type = '', $expires = 0) {
  52. $query = $this->query($sql, $type, $expires);
  53. $r = $this->fetch_array($query);
  54. $this->free_result($query);
  55. return $r ;
  56. }
  57. function counter($table, $condition = '', $type = '', $expires = 0) {
  58. global $cfg;
  59. $table = strpos($table, $cfg['tb_pre']) === false ? $cfg['tb_pre'].$table : $table;
  60. $sql = "select count(*) as num from {$table}";
  61. if($condition) $sql .= " where $condition";
  62. $r = $this->get_one($sql, $type, $expires);
  63. return $r ? $r['num'] : 0;
  64. }
  65. function fetch_array($query, $result_type = mysql_assoc) {
  66. return $this->cache_id ? $this->_fetch_array($query) : @mysql_fetch_array($query, $result_type);
  67. }
  68. function affected_rows() {
  69. return mysql_affected_rows($this->connid);
  70. }
  71. function num_rows($query) {
  72. return mysql_num_rows($query);
  73. }
  74. function num_fields($query) {
  75. return mysql_num_fields($query);
  76. }
  77. function escape_string($str){
  78. return mysql_escape_string($str);
  79. }
  80. function result($query, $row) {
  81. return @mysql_result($query, $row);
  82. }
  83. function free_result($query) {
  84. return @mysql_free_result($query);
  85. }
  86. function insert_id() {
  87. return mysql_insert_id($this->connid);
  88. }
  89. function fetch_row($query) {
  90. return mysql_fetch_row($query);
  91. }
  92. function version() {
  93. return mysql_get_server_info($this->connid);
  94. }
  95. function close() {
  96. return mysql_close($this->connid);
  97. }
  98. function error() {
  99. return @mysql_error($this->connid);
  100. }
  101. function errno() {
  102. return intval(@mysql_errno($this->connid)) ;
  103. }
  104. function halt($message = '', $sql = '') {
  105. global $cfg;
  106. if($message) {
  107. if($cfg['errlog']) {
  108. $log = "query:$sql|errno:".$this->errno()."|error:".$this->error()."|errmsg:$message";
  109. log_write($log, 'sql');
  110. }
  111. }
  112. showmsg("mysqlerror:$message",'-1');
  113. exit();
  114. }
  115. function _query($sql) {
  116. global $fr_time;
  117. $this->cache_file = cache_root.'/sql/'.substr($this->cache_id, 0, 2).'/'.$this->cache_id.'.php教程';
  118. if(!is_file($this->cache_file) || ($fr_time - @filemtime($this->cache_file) > $this->cache_expires)) {
  119. $tmp = array();
  120. $result = $this->query($sql, '', '', true);
  121. while($r = mysql_fetch_array($result, mysql_assoc)) {
  122. $tmp[] = $r;
  123. }
  124. $this->result = $tmp;
  125. $this->free_result($result);
  126. file_put($this->cache_file, "<?php /*".( $fr_time+$this->cache_expires)."*/ return ".var_export($this->result, true).";n?>");
  127. } else {
  128. $this->result = include $this->cache_file;
  129. }
  130. return $this->result;
  131. }
  132. function _fetch_array($query = array()) {
  133. if($query) $this->result = $query;
  134. if(isset($this->result[$this->cursor])) {
  135. return $this->result[$this->cursor++];
  136. } else {
  137. $this->cursor = $this->cache_id = 0;
  138. return array();
  139. }
  140. }
  141. }
  142. function checksql($dbstr,$querytype='select'){
  143. $clean = '';
  144. $old_pos = 0;
  145. $pos = -1;
  146. //普通语句,直接过滤特殊语法
  147. if($querytype=='select'){
  148. $nastr = "/[^0-9a-z@._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@.-]{1,}/i";
  149. if(preg_match($nastr,$dbstr)){
  150. log_write($dbstr,'sql');
  151. showmsg('safeerror:10001', '网页特效:;');
  152. exit();
  153. }
  154. }
  155. //完整的sql检查
  156. while (true){
  157. $pos = strpos($dbstr, ''', $pos + 1);
  158. if ($pos === false){
  159. break;
  160. }
  161. $clean .= substr($dbstr, $old_pos, $pos - $old_pos);
  162. while (true){
  163. $pos1 = strpos($dbstr, ''', $pos + 1);
  164. $pos2 = strpos($dbstr, '', $pos + 1);
  165. if ($pos1 === false){
  166. break;
  167. }
  168. elseif ($pos2 == false || $pos2 > $pos1){
  169. $pos = $pos1;
  170. break;
  171. }
  172. $pos = $pos2 + 1;
  173. }
  174. $clean .= '$s$';
  175. $old_pos = $pos + 1;
  176. }
  177. $clean .= substr($dbstr, $old_pos);
  178. $clean = trim(strtolower(preg_replace(array('~s+~s' ), array(' '), $clean)));
  179. if (strpos($clean, 'union') !== false && preg_match('~(^|[^a-z])union($|[^[a-z])~s', $clean) != 0){
  180. $fail = true;
  181. }
  182. elseif (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, '#') !== false){
  183. $fail = true;
  184. }
  185. elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[a-z])~s', $clean) != 0){
  186. $fail = true;
  187. }
  188. elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0){
  189. $fail = true;
  190. }
  191. elseif (strpos($clean, 'load_file') !== false && preg_match('~(^|[^a-z])load_file($|[^[a-z])~s', $clean) != 0){
  192. $fail = true;
  193. }
  194. elseif (strpos($clean, 'into outfile') !== false && preg_match('~(^|[^a-z])intos+outfile($|[^[a-z])~s', $clean) != 0){
  195. $fail = true;
  196. }
  197. elseif (preg_match('~([^)]*?select~s', $clean) != 0){
  198. $fail = true;
  199. }
  200. if (!emptyempty($fail)){
  201. log_write($dbstr,'sql');
  202. showmsg('safeerror:10002', 'javascript:;');exit;
  203. }//开源代码phpfensi.com
  204. else
  205. {
  206. return $dbstr;
  207. }
  208. }