<?php

//数据库类
class MysqlDb{
	
	public $dbhost;	//主机地址
	public $dbuse;	//用户名
	public $dbpw;	//密码
	public $dbname;	//数据库名
	public $tabpre;	//表名前缀
	public $conn;	//连接
	public $Result;	//查询结果
	
	public function __construct($host, $use, $pw, $name, $pre = ''){
		$this->dbhost = $host;
		$this->dbuse = $use;
		$this->dbpw = $pw;
		$this->dbname = $name;
		$this->tabpre = $pre;
	}
	
	//连接数据库
	public function link_db(){
		$this->conn = mysql_connect($this->dbhost, $this->dbuse, $this->dbpw);
		$conn = $this->conn;
		if($conn == false){
			return false;
		}
		else{
			return $conn;
		}
	}
	
	//选择数据库
	public function sel_db($dbname = null){
		if($dbname == null){
			$dbname = $this->dbname;
		}
		$sel = mysql_select_db($dbname, $this->conn);
		mysql_query("set names 'UTF8'");
		return $sel;
	}
	
	//连接并选择数据库
	public function open_db(){
		if($this->link_db() == false){
			return false;
		}
		else{
			$this->link_db();
			$open_db = mysql_select_db($this->dbname, $this->conn);
			mysql_query("set names 'UTF8'");
			if($open_db == true){
				$link['conn'] = $this->conn;
				$link['sel'] = true;
				return $link;
			}
			else{
				return false;
			}
		}
	}
	
	//关闭连接
	public function close_dblink($conn = null){
		if($conn == null){
			$conn = $this->conn;
		}
		$close = mysql_close($conn);
		return $close;
	}
	
	//内连接查询
	public function JoinQuery($tablename = array(), $ON = null, $condition = null, $conn = null){
		$tabpre = $this->tabpre;
		$having = '';
		$inner = '';
		if(count($tablename) == 2){
			$inner = '`'.$tabpre.$tablename[0].'` inner join '.'`'.$tabpre.$tablename[1].'`';
		}
		else{
			foreach ($tablename as $key => $v){
				$tablename[$key] = $tabpre.$v;
				$inner .= '`'.$tabpre.$tablename[$key].'`, ';
			}
			$inner = rtrim($inner,', ');
		}
		if($condition == null){
			if($ON == null){
				$sql = "SELECT * FROM ".$inner;
			}
			else{
				$sql = "SELECT * FROM ".$inner." ON ".$ON;
			}
		}
		else{
			if($ON == null){
				$sql = "SELECT * FROM ".$inner." ".$condition;
			}
			else{
				$sql = "SELECT * FROM ".$inner." ON ".$ON." ".$condition;
			}
		}
		$query = mysql_query($sql, $this->conn);
		$i = 0;
		$v = null;
		while($row = mysql_fetch_array($query)){
			$v[$i++] = $row;
		}
		$this->Result = $v;
	}
	
	//外连接查询
	public function OuterJoinQuery($c = 'left', $tablename = array(), $ON = null, $condition = null, $conn = null){
		$tabpre = $this->tabpre;
		$having = '';
		$inner = '';
		if(count($tablename) == 2){
			$inner = '`'.$tabpre.$tablename[0].'` '.$c.' outer join '.'`'.$tabpre.$tablename[1].'`';
		}
		else{
			foreach ($tablename as $key => $v){
				$tablename[$key] = $tabpre.$v;
				$inner .= '`'.$tabpre.$tablename[$key].'`, ';
			}
			$inner = rtrim($inner,', ');
		}
		if($condition == null){
			if($ON == null){
				$sql = "SELECT * FROM ".$inner;
			}
			else{
				$sql = "SELECT * FROM ".$inner." ON ".$ON;
			}
		}
		else{
			if($ON == null){
				$sql = "SELECT * FROM ".$inner." ".$condition;
			}
			else{
				$sql = "SELECT * FROM ".$inner." ON ".$ON." ".$condition;
			}
		}
		$query = mysql_query($sql, $this->conn);
		$i = 0;
		$v = null;
		while($row = mysql_fetch_array($query)){
			$v[$i++] = $row;
		}
		$this->Result = $v;
	}	
	
	//使用SELECT语句查询数据
	public function sel($tablename, $condition = null, $conn = null){
		$tabpre = $this->tabpre;
		if($condition == null){
			$sql = "SELECT * FROM `".$tabpre.$tablename."`";
		}
		else{
			$sql = "SELECT * FROM `".$tabpre.$tablename."` ".$condition;
		}
		$query = mysql_query($sql, $this->conn);
		$i = 0;
		$v = null;
		while($row = mysql_fetch_array($query)){
			$v[$i++] = $row;
		}
		$this->Result = $v;
	}
	
	//使用UPDATA语句更新数据库
	public function up($tablename, $up, $condition = null, $conn = null){
		$tabpre = $this->tabpre;
		if($condition == null){
			$sql = "UPDATE `".$tabpre.$tablename."` SET ".$up;
		}
		else{
			$sql = "UPDATE `".$tabpre.$tablename."` SET ".$up." ".$condition;
		}
		if(mysql_query($sql, $this->conn)){
			return true;
		}
		else{
			return false;
		}
	}
	
	//使用Insert语句添加新记录
	public function Add($tablename, $Add = array(), $condition = null, $conn = null){
		$tabpre = $this->tabpre;
		$field = null;
		$value = null;
		foreach ($Add as $Key => $v){
			$field .= "`".$Key."`, ";
			$value .= "'".$v."', ";
		}
		$field = rtrim($field, ", ");
		$value = rtrim($value, ", ");
		$sql = "INSERT INTO `".$tabpre.$tablename."` (".$field.") VALUES (".$value.")";
		if(mysql_query($sql, $this->conn)){
			return true;
		}
		else{
			return false;
		}
	}
	
	//使用DELETE语句执行删除操作
	public function Del($tablename, $condition, $conn = null){
		$tabpre = $this->tabpre;
		$sql = "DELETE FROM `".$tabpre.$tablename."` ".$condition;
		if(mysql_query($sql, $this->conn)){
			return true;
		}
		else{
			return false;
		}
	}
	
	//计算数据库尺寸
	function CalcFullDatabaseSize($database, $db) {
		$tables = mysql_list_tables($database, $db);
		if (!$tables) { return -1; }
		$table_count = mysql_num_rows($tables);
		$size = 0;
		for ($i=0; $i < $table_count; $i++) {
			$tname = mysql_tablename($tables, $i);
			$r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE '".$tname."'");
			$data = mysql_fetch_array($r);
			$size += ($data['Index_length'] + $data['Data_length']);
		};
		$units = array(' B', ' KB', ' MB', ' GB', ' TB');
		for ($i = 0; $size > 1024; $i++) { $size /= 1024; }
		return round($size, 2).$units[$i];
	}
	
	//----------------------------------
}

?>