程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 操作mysql數據庫的類

操作mysql數據庫的類

編輯:關於MYSQL數據庫
  1. class MySQL {
  2.     private $server = "";
  3.     private $user = "";
  4.     private $passWord = "";
  5.     private $database = "";
  6.     private $linkMode = 1;
  7.     private $link_id = 0;
  8.     private $query_id = 0;
  9.     private $query_times = 0;
  10.     private $result = array ();
  11.     private $fetchMode = MySQL_ASSOC;
  12.     private $err_no = 0;
  13.     private $err_msg;
  14.     private $character;
  15.     //======================================
  16.     // 函數: MySQL()
  17.     // 功能: 構造函數
  18.     // 參數: 參數類的變量定義
  19.     // 說明: 構造函數將自動連接數據庫
  20.     // 如果想手動連接去掉自動連接函數
  21.     //======================================
  22.     public function __construct($server, $user, $passWord, $database, $character = "UTF8", $linkMode = 0) {
  23.         if (empty ( $server ) || empty ( $user ) || empty ( $database ))
  24.             $this->halt ( "提交的數據庫信息不完整!請檢查服務器地址,用戶和數據庫是否正確有效" );
  25.         
  26.         $this->server = $server;
  27.         $this->user = $user;
  28.         $this->password = $passWord;
  29.         $this->database = $database;
  30.         $this->linkMode = $linkMode;
  31.         $this->character = $character;
  32.         $this->connect ();
  33.     }
  34.     //======================================
  35.     // 函數: connect($server,$user,$passWord,$database)
  36.     // 功能: 連接數據庫
  37.     // 參數: $server 主機名, $user 用戶名
  38.     // 參數: $passWord 密碼, $database 數據庫名稱
  39.     // 返回: 0:失敗
  40.     // 說明: 默認使用類中變量的初始值
  41.     //======================================
  42.     public function connect($server = "", $user = "", $passWord = "", $database = "") {
  43.         $server = $server ? $server : $this->server;
  44.         $user = $user ? $user : $this->user;
  45.         $password = $password ? $password : $this->passWord;
  46.         $database = $database ? $database : $this->database;
  47.         
  48.         $this->link_id = $this->linkMode ? mysql_pconnect ( $server, $user, $passWord, $database ) : MySQL_connect ( $server, $user, $passWord, $database );
  49.         
  50.         if (! $this->link_id) {
  51.             $this->halt ( "數據庫連接失敗!請檢查各項參數!" );
  52.             return 0;
  53.         }
  54.         
  55.         if (! MySQL_select_db ( $database, $this->link_id )) {
  56.             $this->halt ( "無法選擇數據庫" );
  57.             return 0;
  58.         }
  59.         
  60.         if ($this->character != "GBK" && $this->character != "UTF8") {
  61.             $this->halt ( "輸入的編碼模式不正確!" );
  62.             return 0;
  63.         }
  64.         
  65.         $this->query ( 'SET NAMES ' . $this->character );
  66.         return $this->link_id;
  67.     }
  68.     //======================================
  69.     // 函數: query($sql)
  70.     // 功能: 數據查詢
  71.     // 參數: $sql 要查詢的SQL語句
  72.     // 返回: 0:失敗
  73.     //======================================
  74.     public function query($sql) {
  75.         $this->query_times ++;
  76.         $this->query_id = MySQL_query ( $sql, $this->link_id );
  77.         if (! $this->query_id) {
  78.             $this->halt ( "<font color=red>" . $sql . "</font> 語句執行不成功!" );
  79.             return 0;
  80.         }
  81.         
  82.         return $this->query_id;
  83.     }
  84.     //======================================
  85.     // 函數: setFetchMode($mode)
  86.     // 功能: 設置取得記錄的模式
  87.     // 參數: $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MySQL_BOTH
  88.     // 返回: 0:失敗
  89.     //======================================
  90.     public function setFetchMode($mode) {
  91.         if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MySQL_BOTH) {
  92.             $this->fetchMode = $mode;
  93.             return 1;
  94.         } else {
  95.             $this->halt ( "錯誤的模式." );
  96.             return 0;
  97.         }
  98.     }
  99.     //======================================
  100.     // 函數: fetchRow()
  101.     // 功能: 從記錄集中取出一條記錄
  102.     // 返回: 0: 出錯 record: 一條記錄
  103.     //======================================
  104.     public function fetchRow() {
  105.         $this->record = MySQL_fetch_array ( $this->query_id, $this->fetchMode );
  106.         
  107.         return $this->record;
  108.     }
  109.     //======================================
  110.     // 函數: fetchAll()
  111.     // 功能: 從記錄集中取出所有記錄
  112.     // 返回: 記錄集數組
  113.     //======================================
  114.     public function fetchAll() {
  115.         $arr [] = array ();
  116.         
  117.         while ( $this->record = MySQL_fetch_array ( $this->query_id, $this->fetchMode ) )
  118.             $arr [] = $this->record;
  119.         
  120.         MySQL_free_result ( $this->query_id );
  121.         return $arr;
  122.     }
  123.     //======================================
  124.     // 函數: getValue()
  125.     // 功能: 返回記錄中指定字段的數據
  126.     // 參數: $fIEld 字段名或字段索引
  127.     // 返回: 指定字段的值
  128.     //======================================
  129.     public function getValue($filed) {
  130.         return $this->record [$filed];
  131.     }
  132.     //======================================
  133.     // 函數: getquery_id()
  134.     // 功能: 返回查詢號
  135.     //======================================    
  136.     public function getquery_id() {
  137.         return $this->query_id;
  138.     }
  139.     //======================================
  140.     // 函數: affectedRows()
  141.     // 功能: 返回影響的記錄數
  142.     //======================================    
  143.     public function affectedRows() {
  144.         return MySQL_affected_rows ( $this->link_id );
  145.     }
  146.     //======================================
  147.     // 函數: recordCount()
  148.     // 功能: 返回查詢記錄的總數
  149.     // 參數: 無
  150.     // 返回: 記錄總數
  151.     //======================================    
  152.     public function recordCount() {
  153.         return MySQL_num_rows ( $this->query_id );
  154.     }
  155.     //======================================
  156.     // 函數: getquery_times()
  157.     // 功能: 返回查詢的次數
  158.     // 參數: 無
  159.     // 返回: 查詢的次數
  160.     //======================================    
  161.     public function getquery_times() {
  162.         return $this->query_times;
  163.     }
  164.     //======================================
  165.     // 函數: getVersion()
  166.     // 功能: 返回MySQL的版本
  167.     // 參數: 無
  168.     //======================================    
  169.     public function getVersion() {
  170.         $this->query ( "select version() as ver" );
  171.         $this->fetchRow ();
  172.         return $this->getValue ( "ver" );
  173.     }
  174.     //======================================
  175.     // 函數: getDBSize($database, $tblPrefix=null)
  176.     // 功能: 返回數據庫占用空間大小
  177.     // 參數: $database 數據庫名
  178.     // 參數: $tblPrefix 表的前綴,可選
  179.     //======================================    
  180.     public function getDBSize($database, $tblPrefix = null) {
  181.         $sql = "SHOW TABLE STATUS FROM " . $database;
  182.         if ($tblPrefix != null) {
  183.             $sql .= " LIKE '$tblPrefix%'";
  184.         }
  185.         $this->query ( $sql );
  186.         $size = 0;
  187.         while ( $this->fetchRow () )
  188.             $size += $this->getValue ( "Data_length" ) + $this->getValue ( "Index_length" );
  189.         return $size;
  190.     }
  191.     //======================================
  192.     // 函數: halt($err_msg)
  193.     // 功能: 處理所有出錯信息
  194.     // 參數: $err_msg 自定義的出錯信息
  195.     //=====================================    
  196.     public function halt($err_msg = "") {
  197.         if ($err_msg == "") {
  198.             $this->errno = MySQL_errno ();
  199.             $this->error = MySQL_error ();
  200.             echo "<b>MySQL error:<b><br>";
  201.             echo $this->errno . ":" . $this->error . "<br>";
  202.             exit ();
  203.         } else {
  204.             echo "<b>MySQL error:<b><br>";
  205.             echo $err_msg . "<br>";
  206.             exit ();
  207.         }
  208.     }
  209.     //======================================
  210.     // 函數: insertID()
  211.     // 功能: 返回最後一次插入的自增ID
  212.     // 參數: 無
  213.     //======================================    
  214.     public function insertID() {
  215.         return MySQL_insert_id ();
  216.     }
  217.     //======================================
  218.     //函數:close()
  219.     //功能:關閉非永久的數據庫連接
  220.     //參數:無
  221.     //======================================
  222.     public function close() {
  223.         $link_id = $link_id ? $link_id : $this->link_id;
  224.         MySQL_close ( $link_id );
  225.     }
  226.     //======================================
  227.     // 函數: sqlSelect()
  228.     // 功能: 返回組合的select查詢值
  229.     // 參數: $tbname 查詢的表名
  230.     // 參數: $where 條件
  231.     // 參數: $fIElds 字段值
  232.     // 參數: $orderby 按某字段排序
  233.     // 參數: $sort 正序ASC,倒序DESC,$orderby 不為空是有效
  234.     // 參數: $limit 取得記錄的條數,0,8
  235.     // 返回: 查詢語句
  236.     //======================================
  237.     function sqlSelect($tbname, $where = "", $limit = 0, $fIElds = "*", $orderby = "", $sort = "DESC") {
  238.         $sql = "SELECT " . $fIElds . " FROM " . $tbname . ($where ? " WHERE " . $where : "") . ($orderby ? " ORDER BY " . $orderby . " " . $sort : "") . ($limit ? " limit " . $limit : "");
  239.         return $sql;
  240.     }
  241.     //======================================
  242.     // 函數: sqlInsert()
  243.     // 功能: Insert插入數據函數
  244.     // 參數: $taname 要插入數據的表名
  245.     // 參數: $row 要插入的內容 (數組)
  246.     // 返回: 記錄總數
  247.     // 返回: 插入語句
  248.     //======================================
  249.     function sqlInsert($tbname, $row) {
  250.         foreach ( $row as $key => $value ) {
  251.             $sqlfIEld .= $key . ",";
  252.             $sqlvalue .= "'" . $value . "',";
  253.         }
  254.         return "INSERT INTO " . $tbname . "(" . substr ( $sqlfIEld, 0, - 1 ) . ") VALUES (" . substr ( $sqlvalue, 0, - 1 ) . ")";
  255.     }
  256.     //======================================
  257.     // 函數: sqlUpdate()
  258.     // 功能: Update更新數據的函數
  259.     // 參數: $taname 要插入數據的表名
  260.     // 參數: $row 要插入的內容 (數組)
  261.     // 參數: $where 要插入的內容 的條件
  262.     // 返回: Update語句
  263.     //======================================    
  264.     function sqlUpdate($tbname, $row, $where) {
  265.         foreach ( $row as $key => $value ) {
  266.             $sqlud .= $key . "= '" . $value . "',";
  267.         }
  268.         return "UPDATE " . $tbname . " SET " . substr ( $sqlud, 0, - 1 ) . " WHERE " . $where;
  269.     }
  270.     //======================================
  271.     // 函數: sqlDelete()
  272.     // 功能: 刪除指定條件的行
  273.     // 參數: $taname 要插入數據的表名
  274.     // 參數: $where 要插入的內容 的條件
  275.     // 返回: DELETE語句
  276.     //======================================    
  277.     function sqlDelete($tbname, $where) {
  278.         if (! $where) {
  279.             $this->halt ( "刪除函數沒有指定條件!" );
  280.             return 0;
  281.         }
  282.         return "DELETE FROM " . $tbname . " WHERE " . $where;
  283.     }
  284.     
  285.     //======================================
  286.     //函數:checkSql SQL語句的過濾
  287.     //功能:過濾一些特殊語法
  288.     //參數:$db_string 查詢的SQL語句
  289.     //參數:$querytype 查詢的類型
  290.     //======================================
  291.     function checkSql($db_string, $querytype = 'select') {
  292.         $clean = '';
  293.         $old_pos = 0;
  294.         $pos = - 1;
  295.         
  296.         //如果是普通查詢語句,直接過濾一些特殊語法
  297.         if ($querytype == 'select') {
  298.             $notallow1 = "[^0-9a-z@._-]{1,}(union|sleep|benchmark|load_file|outfile)[^[email protected]]{1,}";
  299.             
  300.             //$notallow2 = "--|/*";
  301.             if (eregi ( $notallow1, $db_string )) {
  302.                 exit ( "<font size='5' color='red'>Safe Alert: Request Error step 1 !</font>" );
  303.             }
  304.         }
  305.         
  306.         //完整的SQL檢查
  307.         while ( true ) {
  308.             $pos = strpos ( $db_string, ''', $pos + 1 );
  309.             if ($pos === false) {
  310.                 break;
  311.             }
  312.             $clean .= substr ( $db_string, $old_pos, $pos - $old_pos );
  313.             while ( true ) {
  314.                 $pos1 = strpos ( $db_string, ''', $pos + 1 );
  315.                 $pos2 = strpos ( $db_string, '\', $pos + 1 );
  316.                 if ($pos1 === false) {
  317.                     break;
  318.                 } elseif ($pos2 == false || $pos2 > $pos1) {
  319.                     $pos = $pos1;
  320.                     break;
  321.                 }
  322.                 $pos = $pos2 + 1;
  323.             }
  324.             $clean .= '$s$';
  325.             $old_pos = $pos + 1;
  326.         }
  327.         $clean .= substr ( $db_string, $old_pos );
  328.         $clean = trim ( strtolower ( preg_replace ( array ('~s+~s' ), array (' ' ), $clean ) ) );
  329.         
  330.         //老版本的MySQL並不支持union,常用的程序裡也不使用union,但是一些黑客使用它,所以檢查它
  331.         if (strpos ( $clean, 'union' ) !== false && preg_match ( '~(^|[^a-z])union($|[^[a-z])~s', $clean ) != 0) {
  332.             $fail = true;
  333.         }
  334.         //發布版本的程序可能比較少包括--,#這樣的注釋,但是黑客經常使用它們
  335.         elseif (strpos ( $clean, '/*' ) > 2 || strpos ( $clean, '--' ) !== false || strpos ( $clean, '#' ) !== false) {
  336.             $fail = true;
  337.         }
  338.         //這些函數不會被使用,但是黑客會用它來操作文件,down掉數據庫
  339.         elseif (strpos ( $clean, 'sleep' ) !== false && preg_match ( '~(^|[^a-z])sleep($|[^[a-z])~s', $clean ) != 0) {
  340.             $fail = true;
  341.         } elseif (strpos ( $clean, 'benchmark' ) !== false && preg_match ( '~(^|[^a-z])benchmark($|[^[a-z])~s', $clean ) != 0) {
  342.             $fail = true;
  343.         } elseif (strpos ( $clean, 'load_file' ) !== false && preg_match ( '~(^|[^a-z])load_file($|[^[a-z])~s', $clean ) != 0) {
  344.             $fail = true;
  345.         } elseif (strpos ( $clean, 'into outfile' ) !== false && preg_match ( '~(^|[^a-z])intos+outfile($|[^[a-z])~s', $clean ) != 0) {
  346.             $fail = true;
  347.         }
  348.         //老版本的MySQL不支持子查詢,我們的程序裡可能也用得少,但是黑客可以使用它來查詢數據庫敏感信息
  349.         elseif (preg_match ( '~([^)]*?select~s', $clean ) != 0) {
  350.             $fail = true;
  351.         }
  352.         if (! empty ( $fail )) {
  353.             exit ( "<font size='5' color='red'>Safe Alert: Request Error step 2!</font>" );
  354.         } else {
  355.             return $db_string;
  356.         }
  357.     }
  358.     //======================================
  359.     //函數:析構函數
  360.     //功能:釋放類,關閉非永久的數據庫連接
  361.     //參數:無
  362.     //======================================
  363.     public function __destruct() {
  364.         $this->close ();
  365.     }
  366. }
  367. ?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved