php连接mysql数据库mysql.class.php
本文章是一款比较实例的php 连接mysql数据库的连接类,比起一般的php mysql数据库函数要实用方法了很多,操作维护起来也很简单,只要处理这一个文伯就KO了,实例类代码如下:
-
-
-
-
-
-
-
- class mysql {
- private $server;
- private $user;
- private $password;
- private $database;
- private $link;
- private $charset = "utf8";
-
-
-
-
-
-
- function __construct($server, $user, $password, $database, $charset) {
- $this->server = $server;
- $this->user = $user;
- $this->password = $password;
- $this->database = $database;
- $this->charset = $charset;
- $this->connect();
- }
-
-
-
-
-
-
- function connect() {
- $this->link = mysql_connect($this->server, $this->user, $this->password) or die($this->error("数据库服务器连接出错!"));
- mysql_select_db($this->database, $this->link) or die($this->error("数据库连接出错!"));
- mysql_query("set names '$this->charset'");
- }
-
-
-
-
-
-
- function query($sql) {
- $result = mysql_query($sql, $this->link);
- if (!$result) {
- $this->error($sql . "语句执行失败!");
- return false;
- } else {
- return $result;
- }
- }
-
-
-
-
-
-
-
- function fetcharray($result) {
- return mysql_fetch_array($result);
- }
-
-
-
-
-
-
- function fetchall($result) {
- $arr[] = array ();
- while ($row = mysql_fetch_array($result)) {
- $arr[] = $row;
- }
- mysql_free_result($result);
- return $arr;
- }
-
-
-
-
-
-
- function numrows($result) {
- return mysql_num_rows($result);
- }
-
-
-
-
-
-
- function numfields($result) {
- return mysql_num_fields($result);
- }
-
-
-
-
-
-
- function affectedrows() {
- return mysql_affected_rows($this->link);
- }
-
-
-
-
-
-
- function version() {
- return mysql_get_server_info();
- }
-
-
-
-
-
-
- function insertid() {
- return mysql_insert_id($this->link);
- }
-
-
-
-
-
-
-
- function checksql($db_string, $querytype = 'select') {
- $clean = '';
- $old_pos = 0;
- $pos = - 1;
-
- if ($querytype == 'select') {
- $notallow1 = "[^0-9a-z@._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@.-]{1,}";
-
- if (eregi ( $notallow1, $db_string )) {
- exit ( "<font size='5' color='red'>safe alert: request error step 1 !</font>" );
- }
- }
-
- while ( true ) {
- $pos = strpos ( $db_string, ''', $pos + 1 );
- if ($pos === false) {
- break;
- }
- $clean .= substr ( $db_string, $old_pos, $pos - $old_pos );
- while ( true ) {
- $pos1 = strpos ( $db_string, ''', $pos + 1 );
- $pos2 = strpos ( $db_string, '', $pos + 1 );
- if ($pos1 === false) {
- break;
- } elseif ($pos2 == false || $pos2 > $pos1) {
- $pos = $pos1;
- break;
- }
- $pos = $pos2 + 1;
- }
- $clean .= '$s$';
- $old_pos = $pos + 1;
- }
- $clean .= substr ( $db_string, $old_pos );
- $clean = trim ( strtolower ( preg_replace ( array ('~s+~s' ), array (' ' ), $clean ) ) );
-
- if (strpos ( $clean, 'union' ) !== false && preg_match ( '~(^|[^a-z])union($|[^[a-z])~s', $clean ) != 0) {
- $fail = true;
- }
-
- elseif (strpos ( $clean, '/*' ) > 2 || strpos ( $clean, '--' ) !== false || strpos ( $clean, '#' ) !== false) {
- $fail = true;
- }
-
- elseif (strpos ( $clean, 'sleep' ) !== false && preg_match ( '~(^|[^a-z])sleep($|[^[a-z])~s', $clean ) != 0) {
- $fail = true;
- } elseif (strpos ( $clean, 'benchmark' ) !== false && preg_match ( '~(^|[^a-z])benchmark($|[^[a-z])~s', $clean ) != 0) {
- $fail = true;
- } elseif (strpos ( $clean, 'load_file' ) !== false && preg_match ( '~(^|[^a-z])load_file($|[^[a-z])~s', $clean ) != 0) {
- $fail = true;
- } elseif (strpos ( $clean, 'into outfile' ) !== false && preg_match ( '~(^|[^a-z])intos+outfile($|[^[a-z])~s', $clean ) != 0) {
- $fail = true;
- }
-
- elseif (preg_match ( '~([^)]*?select~s', $clean ) != 0) {
- $fail = true;
- }
- if (! emptyempty ( $fail )) {
- exit ( "<font size='5' color='red'>safe alert: request error step 2!</font>" );
- } else {
- return $db_string;
- }
- }
- /*===================================================
- * 方法:close
- * 功能:关闭连接
- * 参数:无
- * 说明:关闭非永久数据库连接
- ==================================================*/
- function close() {
- mysql_close($this->link);
- }
-
-
-
-
-
-
- function error($err_msg = "") {
- if ($err_msg == "") {
- echo "errno:" . mysql_errno . "</br>";
- echo "error:" . mysql_error . "</br>";
- } else {
- echo $err_msg;
- }
- }
-
-
-
-
-
-
- function __destruct() {
- $this->close();
- }
- }
-
- function conn_db(){
- $link_db=new mysql(web_server,web_user,web_pwd,web_db,"utf8");
- return $link_db;
- }