php5 操作MYSQL的类
<?php
/** 操作mysql数据库的类 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; //====================================== // 函数: mysql() // 功能: 构造函数 // 参数: 参数类的变量定义 // 说明: 构造函数将自动连接数据库 // 如果想手动连接去掉自动连接函数 //====================================== public function __construct($server,$user,$password,$database,$linkMode=0) { if( empty($server) || empty($user) || empty($database) ) $this->halt("提交的数据库信息不完整!请检查服务器地址,用户和数据库是否正确有效"); $this->server = $server; $this->user = $user; $this->password = $password; $this->database = $database; $this->linkMode = $linkMode; $this->connect(); } //====================================== // 函数: connect($server,$user,$password,$database) // 功能: 连接数据库 // 参数: $server 主机名, $user 用户名 // 参数: $password 密码, $database 数据库名称 // 返回: 0:失败 // 说明: 默认使用类中变量的初始值 //====================================== 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("数据库连接失败!请检查各项参数!"); return 0; } if (!mysql_select_db($database, $this->link_id)) { $this->halt("无法选择数据库"); return 0; } return $this->link_id; } //====================================== // 函数: query($sql) // 功能: 数据查询 // 参数: $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("执行不成功!"); return 0; } return $this->query_id; } //====================================== // 函数: setFetchMode($mode) // 功能: 设置取得记录的模式 // 参数: $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; } } //====================================== // 函数: fetchRow() // 功能: 从记录集中取出一条记录 // 返回: 0: 出错 record: 一条记录 //====================================== public function fetchRow() { $this->record = mysql_fetch_array($this->query_id,$this->fetchMode); return $this->record; } //====================================== // 函数: fetchAll() // 功能: 从记录集中取出所有记录 // 返回: 记录集数组 //====================================== 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; } //====================================== // 函数: getValue() // 功能: 返回记录中指定字段的数据 // 参数: $field 字段名或字段索引 // 返回: 指定字段的值 //====================================== public function getValue($filed) { return $this->record[$filed]; } //====================================== // 函数: getquery_id() // 功能: 返回查询号 //====================================== public function getquery_id() { return $this->query_id; } //====================================== // 函数: affectedRows() // 功能: 返回影响的记录数 //====================================== public function affectedRows() { return mysql_affected_rows($this->link_id); } //====================================== // 函数: recordCount() // 功能: 返回查询记录的总数 // 参数: 无 // 返回: 记录总数 //====================================== public function recordCount() { return mysql_num_rows($this->query_id); } //====================================== // 函数: getquery_times() // 功能: 返回查询的次数 // 参数: 无 // 返回: 查询的次数 //====================================== public function getquery_times() { return $this->query_times; } //====================================== // 函数: getVersion() // 功能: 返回mysql的版本 // 参数: 无 //====================================== public function getVersion() { $this->query("select version() as ver"); $this->fetchRow(); $this->getValue("ver"); } //====================================== // 函数: getDBSize($database, $tblPrefix=null) // 功能: 返回数据库占用空间大小 // 参数: $database 数据库名 // 参数: $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; } //====================================== // 函数: halt($err_msg) // 功能: 处理所有出错信息 // 参数: $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; } } //====================================== // 函数: insertID() // 功能: 返回最后一次插入的自增ID // 参数: 无 //====================================== public function insertID() { return mysql_insert_id(); } //====================================== //函数:close() //功能:关闭非永久的数据库连接 //参数:连接ID //====================================== public function close($link_id) { $link_id = $link_id ? $link_id : $this->link_id; mysql_close($link_id); } //====================================== //函数:析构函数 //功能:释放类 //参数:无 //====================================== public function __destruct() { //echo "class disconstructed"; } } ?> |
|