VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php连接mysql数据库mysql.class.php

本文章是一款比较实例的php 连接mysql数据库的连接类,比起一般的php mysql数据库函数要实用方法了很多,操作维护起来也很简单,只要处理这一个文伯就KO了,实例类代码如下:

  1. /* 
  2. ​+----------------------------------------------------------------------- 
  3. | 文件概要:php连接mysql数据库 
  4. | 文件名称:mysql.class.php 
  5. | 创建时间:2010-9-7 
  6. +----------------------------------------------------------------------- 
  7. */ 
  8. class mysql { 
  9.  private $server//服务器名 
  10.  private $user//数据库用户名 
  11.  private $password//数据库密码 
  12.  private $database//数据库名 
  13.  private $link//mysql连接标识符 
  14.  private $charset = "utf8"//数据库编码,默认为utf8 
  15.  /*===================================================== 
  16.   * 方法:__construct 
  17.   * 功能:构造函数 
  18.   * 参数:$server,$user,$password,$database,$charset 
  19.   * 说明:实例化时自动连接数据库. 
  20.   ====================================================*/ 
  21.  function __construct($server$user$password$database$charset) { 
  22.   $this->server = $server
  23.   $this->user = $user
  24.   $this->password = $password
  25.   $this->database = $database
  26.   $this->charset = $charset
  27.   $this->connect(); 
  28.  } 
  29.  /*==================================================== 
  30.   * 方法:connect 
  31.   * 功能:连接数据库 
  32.   * 参数:无 
  33.   * 说明:连接mysql服务器,连接数据库,设置字符编码 
  34.   ===================================================*/ 
  35.  function connect() { 
  36.   $this->link = mysql_connect($this->server, $this->user, $this->password) or die($this->error("数据库服务器连接出错!")); 
  37.   mysql_select_db($this->database, $this->link) or die($this->error("数据库连接出错!")); 
  38.   mysql_query("set names '$this->charset'"); 
  39.  } 
  40.  /*=================================================== 
  41.   * 方法:query 
  42.   * 功能:执行sql 
  43.   * 参数:$sql 
  44.   * 说明:对传过来的sql语句执行,并返回结果$result资源标识符 
  45.   ==================================================*/ 
  46.  function query($sql) { 
  47.   $result = mysql_query($sql$this->link); 
  48.   if (!$result) { 
  49.    $this->error($sql . "语句执行失败!"); 
  50.    return false; 
  51.   } else { 
  52.    return $result
  53.   } 
  54.  }  
  55.  
  56.  /*=================================================== 
  57.   * 方法:fetcharray 
  58.   * 功能:从结果集中取一行做为数组 
  59.   * 参数:$result资源标识符 
  60.   * 说明:需要提供sql语句执行返回的资源标识符 
  61.   ==================================================*/ 
  62.  function fetcharray($result) { 
  63.   return mysql_fetch_array($result); 
  64.  } 
  65.  /*=================================================== 
  66.   * 方法:fetchall 
  67.   * 功能:从结果集中取出所有记录做为二维数组$arr 
  68.   * 参数:$result资源标识符 
  69.   * 说明:循环取所有记录保存为$arr 
  70.   ==================================================*/ 
  71.  function fetchall($result) { 
  72.   $arr[] = array (); 
  73.   while ($row = mysql_fetch_array($result)) { 
  74.    $arr[] = $row
  75.   } 
  76.   mysql_free_result($result); 
  77.   return $arr
  78.  } 
  79.  /*=================================================== 
  80.   * 方法:numrows 
  81.   * 功能:统计结果集中记录数 
  82.   * 参数:$result资源标识符 
  83.   * 说明:统计行数 
  84.   ==================================================*/ 
  85.  function numrows($result) { 
  86.   return mysql_num_rows($result); 
  87.  } 
  88.  /*=================================================== 
  89.   * 方法:numfields 
  90.   * 功能:统计结果集中字段数 
  91.   * 参数:$result资源标识符 
  92.   * 说明:统计字段数 
  93.   ==================================================*/ 
  94.  function numfields($result) { 
  95.   return mysql_num_fields($result); 
  96.  } 
  97.  /*=================================================== 
  98.   * 方法:affectedrows 
  99.   * 功能:取得前一次mysql操作所影响的记录行数 
  100.   * 参数:无 
  101.   * 说明:取得前一次mysql操作所影响的记录行数 
  102.   ==================================================*/ 
  103.  function affectedrows() { 
  104.   return mysql_affected_rows($this->link); 
  105.  } 
  106.  /*=================================================== 
  107.   * 方法:version 
  108.   * 功能:取得mysql版本 
  109.   * 参数:无 
  110.   * 说明:取得当前数据库服务器mysql的版本 
  111.   ==================================================*/ 
  112.  function version() { 
  113.   return mysql_get_server_info(); 
  114.  } 
  115.  /*=================================================== 
  116.   * 方法:insertid 
  117.   * 功能:取得上一步insert操作产生的id 
  118.   * 参数:无 
  119.   * 说明:取得上一步insert操作产生的自增字段id 
  120.   ==================================================*/ 
  121.  function insertid() { 
  122.   return mysql_insert_id($this->link); 
  123.  } 
  124.  
  125.  /*=================================================== 
  126.   * 方法:checksql 
  127.   * 功能:检查sql语句 
  128.   * 参数:sql语句 
  129.   * 说明:关闭非永久数据库连接 
  130.   ==================================================*/ 
  131.  function checksql($db_string$querytype = 'select') { 
  132.         $clean = ''
  133.         $old_pos = 0; 
  134.         $pos = - 1; 
  135.         //如果是普通查询语句,直接过滤一些特殊语法 
  136.         if ($querytype == 'select') { 
  137.             $notallow1 = "[^0-9a-z@._-]{1,}(union|sleep|benchmark|load_file|outfile)[^0-9a-z@.-]{1,}"
  138.             //$notallow2 = "--|/*"; 
  139.             if (eregi ( $notallow1$db_string )) { 
  140.                 exit ( "<font size='5' color='red'>safe alert: request error step 1 !</font>" ); 
  141.             } 
  142.         } 
  143.         //完整的sql检查 
  144.         while ( true ) { 
  145.             $pos = strpos ( $db_string''', $pos + 1 ); 
  146.             if ($pos === false) { 
  147.                 break
  148.             } 
  149.             $clean .= substr ( $db_string$old_pos$pos - $old_pos ); 
  150.             while ( true ) { 
  151.                 $pos1 = strpos ( $db_string''', $pos + 1 ); 
  152.                 $pos2 = strpos ( $db_string''$pos + 1 ); 
  153.                 if ($pos1 === false) { 
  154.                     break
  155.                 } elseif ($pos2 == false || $pos2 > $pos1) { 
  156.                     $pos = $pos1
  157.                     break
  158.                 } 
  159.                 $pos = $pos2 + 1; 
  160.             } 
  161.             $clean .= '$s$'
  162.             $old_pos = $pos + 1; 
  163.         } 
  164.         $clean .= substr ( $db_string$old_pos ); 
  165.         $clean = trim ( strtolower ( preg_replace ( array ('~s+~s' ), array (' ' ), $clean ) ) ); 
  166.         //老版本的mysql并不支持union,常用的程序里也不使用union,但是一些黑客使用它,所以检查它 
  167.         if (strpos ( $clean'union' ) !== false && preg_match ( '~(^|[^a-z])union($|[^[a-z])~s'$clean ) != 0) { 
  168.             $fail = true; 
  169.         } 
  170.         //发布版本的程序可能比较少包括--,#这样的注释,但是黑客经常使用它们 
  171.         elseif (strpos ( $clean'/*' ) > 2 || strpos ( $clean'--' ) !== false || strpos ( $clean'#' ) !== false) { 
  172.             $fail = true; 
  173.         } 
  174.         //这些函数不会被使用,但是黑客会用它来操作文件,down掉数据库 
  175.         elseif (strpos ( $clean'sleep' ) !== false && preg_match ( '~(^|[^a-z])sleep($|[^[a-z])~s'$clean ) != 0) { 
  176.             $fail = true; 
  177.         } elseif (strpos ( $clean'benchmark' ) !== false && preg_match ( '~(^|[^a-z])benchmark($|[^[a-z])~s'$clean ) != 0) { 
  178.             $fail = true; 
  179.         } elseif (strpos ( $clean'load_file' ) !== false && preg_match ( '~(^|[^a-z])load_file($|[^[a-z])~s'$clean ) != 0) { 
  180.             $fail = true; 
  181.         } elseif (strpos ( $clean'into outfile' ) !== false && preg_match ( '~(^|[^a-z])intos+outfile($|[^[a-z])~s'$clean ) != 0) { 
  182.             $fail = true; 
  183.         } 
  184.         //老版本的mysql不支持子查询,我们的程序里可能也用得少,但是黑客可以使用它来查询数据库敏感信息 
  185.         elseif (preg_match ( '~([^)]*?select~s'$clean ) != 0) { 
  186.             $fail = true; 
  187.         } 
  188.         if (! emptyempty ( $fail )) { 
  189.             exit ( "<font size='5' color='red'>safe alert: request error step 2!</font>" ); 
  190.         } else { 
  191.             return $db_string
  192.         } 
  193.     } 
  194.  /*=================================================== 
  195.   * 方法:close 
  196.   * 功能:关闭连接 
  197.   * 参数:无 
  198.   * 说明:关闭非永久数据库连接 
  199.   ==================================================*/ 
  200.  function close() { 
  201.   mysql_close($this->link); 
  202.  } 
  203.  /*=================================================== 
  204.   * 方法:error 
  205.   * 功能:提示错误 
  206.   * 参数:$err_msg 
  207.   * 说明:对给出的错误提示内容给予echo 
  208.   ==================================================*/ 
  209.  function error($err_msg = "") { 
  210.   if ($err_msg == "") { 
  211.    echo "errno:" . mysql_errno . "</br>"
  212.    echo "error:" . mysql_error . "</br>"
  213.   } else { 
  214.    echo $err_msg
  215.   } 
  216.  } 
  217.  /*=================================================== 
  218.   * 方法:__destruct 
  219.   * 功能:析构函数 
  220.   * 参数:无 
  221.   * 说明:释放类,关闭连接 
  222.   ==================================================*/ 
  223.  function __destruct() { 
  224.   $this->close(); 
  225.  } 
  226. //数据库连接函数 
  227. function conn_db(){ 
  228.  $link_db=new mysql(web_server,web_user,web_pwd,web_db,"utf8");//开源代码phpfensi.com 
  229.  return $link_db
  230. }
  231.  




出处:http://www.phpfensi.com/php/20140911/5375.html


相关教程