php5 操作MYSQL的類
<?php
/** 操作mysql數(shù)據(jù)庫的類 Author:SmileAgain Creatdate:2005-9-14 LastModify:2006-03-1 */ class mysql{ private $server = ""; private $user = ""; private $password = ""; private $database; private $linkMode=1; private $link_id = 0; private $query_id =0; private $query_times = 0; private $result = array(); private $fetchMode = MYSQL_ASSOC; private $err_no = 0; private $err_msg; //====================================== // 函數(shù): mysql() // 功能: 構(gòu)造函數(shù) // 參數(shù): 參數(shù)類的變量定義 // 說明: 構(gòu)造函數(shù)將自動連接數(shù)據(jù)庫 // 如果想手動連接去掉自動連接函數(shù) //====================================== public function __construct($server,$user,$password,$database,$linkMode=0) { if( empty($server) || empty($user) || empty($database) ) $this->halt("提交的數(shù)據(jù)庫信息不完整!請檢查服務(wù)器地址,用戶和數(shù)據(jù)庫是否正確有效"); $this->server = $server; $this->user = $user; $this->password = $password; $this->database = $database; $this->linkMode = $linkMode; $this->connect(); } //====================================== // 函數(shù): connect($server,$user,$password,$database) // 功能: 連接數(shù)據(jù)庫 // 參數(shù): $server 主機名, $user 用戶名 // 參數(shù): $password 密碼, $database 數(shù)據(jù)庫名稱 // 返回: 0:失敗 // 說明: 默認(rèn)使用類中變量的初始值 //====================================== public function connect($server = "",$user = "",$password = "" ,$database = "") { $server = $server ? $server : $this->server; $user = $user ? $user : $this->user; $password = $password ? $password : $this->password; $database = $database ? $database : $this->database; $this->link_id = $this->linkMode ? mysql_pconnect($server, $user, $password, $database) : mysql_connect($server, $user, $password, $database); if(!$this->link_id) { $this->halt("數(shù)據(jù)庫連接失?。≌垯z查各項參數(shù)!"); return 0; } if (!mysql_select_db($database, $this->link_id)) { $this->halt("無法選擇數(shù)據(jù)庫"); return 0; } return $this->link_id; } //====================================== // 函數(shù): query($sql) // 功能: 數(shù)據(jù)查詢 // 參數(shù): $sql 要查詢的SQL語句 // 返回: 0:失敗 //====================================== public function query($sql) { $this->query_times++; $this->query_id = mysql_query($sql, $this->link_id); if( !$this->query_id) { $this->halt("執(zhí)行不成功!"); return 0; } return $this->query_id; } //====================================== // 函數(shù): setFetchMode($mode) // 功能: 設(shè)置取得記錄的模式 // 參數(shù): $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH // 返回: 0:失敗 //====================================== public function setFetchMode($mode) { if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MYSQL_BOTH) { $this->fetchMode = $mode; return 1; } else { $this->halt("錯誤的模式."); return 0; } } //====================================== // 函數(shù): fetchRow() // 功能: 從記錄集中取出一條記錄 // 返回: 0: 出錯 record: 一條記錄 //====================================== public function fetchRow() { $this->record = mysql_fetch_array($this->query_id,$this->fetchMode); return $this->record; } //====================================== // 函數(shù): fetchAll() // 功能: 從記錄集中取出所有記錄 // 返回: 記錄集數(shù)組 //====================================== public function fetchAll() { $arr[] = array(); while($this->record = mysql_fetch_array($this->query_id, $this->fetchMode)) $arr[] = $this->record; mysql_free_result($this->query_id); return $arr; } //====================================== // 函數(shù): getValue() // 功能: 返回記錄中指定字段的數(shù)據(jù) // 參數(shù): $field 字段名或字段索引 // 返回: 指定字段的值 //====================================== public function getValue($filed) { return $this->record[$filed]; } //====================================== // 函數(shù): getquery_id() // 功能: 返回查詢號 //====================================== public function getquery_id() { return $this->query_id; } //====================================== // 函數(shù): affectedRows() // 功能: 返回影響的記錄數(shù) //====================================== public function affectedRows() { return mysql_affected_rows($this->link_id); } //====================================== // 函數(shù): recordCount() // 功能: 返回查詢記錄的總數(shù) // 參數(shù): 無 // 返回: 記錄總數(shù) //====================================== public function recordCount() { return mysql_num_rows($this->query_id); } //====================================== // 函數(shù): getquery_times() // 功能: 返回查詢的次數(shù) // 參數(shù): 無 // 返回: 查詢的次數(shù) //====================================== public function getquery_times() { return $this->query_times; } //====================================== // 函數(shù): getVersion() // 功能: 返回mysql的版本 // 參數(shù): 無 //====================================== public function getVersion() { $this->query("select version() as ver"); $this->fetchRow(); $this->getValue("ver"); } //====================================== // 函數(shù): getDBSize($database, $tblPrefix=null) // 功能: 返回數(shù)據(jù)庫占用空間大小 // 參數(shù): $database 數(shù)據(jù)庫名 // 參數(shù): $tblPrefix 表的前綴,可選 //====================================== public function getDBSize($database, $tblPrefix=null) { $sql = "SHOW TABLE STATUS FROM " . $database; if($tblPrefix != null) { $sql .= " LIKE '$tblPrefix%'"; } $this->query($sql); $size = 0; while($this->fetchRow()) $size += $this->getValue("Data_length") + $this->getValue("Index_length"); return $size; } //====================================== // 函數(shù): halt($err_msg) // 功能: 處理所有出錯信息 // 參數(shù): $err_msg 自定義的出錯信息 //===================================== public function halt($err_msg="") { if ("" == $err_msg) { $this->errno = mysql_errno(); $this->error = mysql_error(); echo "<b>mysql error:<b><br>"; echo $this->errno.":".$this->error."<br>"; exit; } else { echo "<b>mysql error:<b><br>"; echo $err_msg."<br>"; exit; } } //====================================== // 函數(shù): insertID() // 功能: 返回最后一次插入的自增ID // 參數(shù): 無 //====================================== public function insertID() { return mysql_insert_id(); } //====================================== //函數(shù):close() //功能:關(guān)閉非永久的數(shù)據(jù)庫連接 //參數(shù):連接ID //====================================== public function close($link_id) { $link_id = $link_id ? $link_id : $this->link_id; mysql_close($link_id); } //====================================== //函數(shù):析構(gòu)函數(shù) //功能:釋放類 //參數(shù):無 //====================================== public function __destruct() { //echo "class disconstructed"; } } ?> |
|