<?php
define('InEmpireCMSDbSql',TRUE);

//------------------------- 數據庫 -------------------------

//鏈接數據庫
function do_dbconnect($dbhost,$dbport,$dbusername,$dbpassword,$dbname){
	global $ecms_config;
	$dblocalhost=$dbhost;
	//端口
	if($dbport)
	{
		$dblocalhost.=':'.$dbport;
	}
	$dblink=@mysqli_connect($dblocalhost,$dbusername,$dbpassword);
	if(!$dblink)
	{
		echo"Cann't connect to DB!";
		exit();
	}
	//編碼
	if($ecms_config['db']['dbver']>='4.1')
	{
		$q='';
		if($ecms_config['db']['setchar'])
		{
			$q='character_set_connection='.$ecms_config['db']['setchar'].',character_set_results='.$ecms_config['db']['setchar'].',character_set_client=binary';
		}
		if($ecms_config['db']['dbver']>='5.0')
		{
			$q.=(empty($q)?'':',').'sql_mode=\'\'';
		}
		if($q)
		{
			@mysqli_query($dblink,'SET '.$q);
		}
	}
	@mysqli_select_db($dblink,$dbname);
	return $dblink;
}

//關閉數據庫
function do_dbclose($dblink){
	if($dblink)
	{
		@mysqli_close($dblink);
	}
}

//設置編碼
function do_DoSetDbChar($dbchar,$dblink){
	@mysqli_query($dblink,'set character_set_connection='.$dbchar.',character_set_results='.$dbchar.',character_set_client=binary;');
}

//取得mysql版本
function do_eGetDBVer($selectdb=0){
	global $empire,$link;
	if($selectdb&&$empire)
	{
		$getdbver=$empire->egetdbver();
	}
	else
	{
		if($link)
		{
			$getdbver=@mysqli_get_server_info($link);
		}
		else
		{
			$getdbver='';
		}
	}
	return $getdbver;
}

//普通操作
function do_dbconnect_common($dbhost,$dbport,$dbusername,$dbpassword,$dbname=''){
	global $ecms_config;
	$dblocalhost=$dbhost;
	//端口
	if($dbport)
	{
		$dblocalhost.=':'.$dbport;
	}
	$dblink=@mysqli_connect($dblocalhost,$dbusername,$dbpassword);
	return $dblink;
}

function do_dbquery_common($query,$dblink,$ecms=0){
	global $ecms_config;
	if($ecms==0)
	{
		$sql=mysqli_query($dblink,$query);
	}
	else
	{
		$sql=mysqli_query($dblink,$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error($dblink).'<br>'.$query):'DbError');
	}
	return $sql;
}

function do_dbfetch_common($sql){
	$r=mysqli_fetch_array($sql);
	return $r;
}

function do_dblastid_common($dblink){
	$id=mysqli_insert_id($dblink);
	if($id<0)
	{
		$sql=do_dbquery_common('SELECT last_insert_id() as total',$dblink);
		$r=do_dbfetch_common($sql);
		$id=$r['total'];
	}
	return $id;
}

//選擇數據庫
function do_eUseDb($dbname,$dblink,$query=0){
	if($query)
	{
		$usedb=do_dbquery_common('use `'.$dbname.'`',$dblink);
	}
	else
	{
		$usedb=@mysqli_select_db($dblink,$dbname);
	}
	return $usedb;
}



//------------------------- 數據庫操作 -------------------------

class mysqlquery
{
	var $dblink;
	var $sql;//sql語句執行結果
	var $query;//sql語句
	var $num;//返回記錄數
	var $r;//返回數組
	var $id;//返回數據庫id號
	//執行mysql_query()語句
	function query($query){
		global $ecms_config;
		$this->sql=mysqli_query(return_dblink($query),$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error(return_dblink($query)).'<br>'.$query):'DbError');
		return $this->sql;
	}
	//執行mysql_query()語句2
	function query1($query){
		$this->sql=mysqli_query(return_dblink($query),$query);
		return $this->sql;
	}
	//執行mysql_query()語句(選擇數據庫USE)
	function usequery($query){
		global $ecms_config;
		$this->sql=mysqli_query($GLOBALS['link'],$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error($GLOBALS['link']).'<br>'.$query):'DbError');
		if($GLOBALS['linkrd'])
		{
			mysqli_query($GLOBALS['linkrd'],$query);
		}
		return $this->sql;
	}
	//執行mysql_query()語句(操作數據庫)
	function updatesql($query){
		global $ecms_config;
		$this->sql=mysqli_query(return_dblink($query),$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error(return_dblink($query)).'<br>'.$query):'DbError');
		return $this->sql;
	}
	//執行mysql_fetch_array()
	function fetch($sql)//此方法的參數是$sql就是sql語句執行結果
	{
		$this->r=mysqli_fetch_array($sql);
		return $this->r;
	}
	//執行fetchone(mysql_fetch_array())
	//此方法與fetch()的區別是:1、此方法的參數是$query就是sql語句 
	//2、此方法用於while(),for()數據庫指針不會自動下移，而fetch()可以自動下移。
	function fetch1($query)
	{
		$this->sql=$this->query($query);
		$this->r=mysqli_fetch_array($this->sql);
		return $this->r;
	}
	//執行mysql_num_rows()
	function num($query)//此類的參數是$query就是sql語句
	{
		$this->sql=$this->query($query);
		$this->num=mysqli_num_rows($this->sql);
		return $this->num;
	}
	//執行numone(mysql_num_rows())
	//此方法與num()的區別是：1、此方法的參數是$sql就是sql語句的執行結果。
	function num1($sql)
	{
		$this->num=mysqli_num_rows($sql);
		return $this->num;
	}
	//執行numone(mysql_num_rows())
	//統計記錄數
	function gettotal($query)
	{
		$this->r=$this->fetch1($query);
		return $this->r['total'];
	}
	//執行free(mysql_result_free())
	//此方法的參數是$sql就是sql語句的執行結果。只有在用到mysql_fetch_array的情況下用
	function free($sql)
	{
		mysqli_free_result($sql);
	}
	//執行seek(mysql_data_seek())
	//此方法的參數是$sql就是sql語句的執行結果,$pit為執行指針的偏移數
	function seek($sql,$pit)
	{
		mysqli_data_seek($sql,$pit);
	}
	//執行id(mysql_insert_id())
	function lastid()//取得最後一次執行mysql數據庫id號
	{
		$this->id=mysqli_insert_id($GLOBALS['link']);
		if($this->id<0)
		{
			$this->id=$this->gettotal('SELECT last_insert_id() as total');
		}
		return $this->id;
	}
	//返回影響數量(mysql_affected_rows())
	function affectnum()//取得操作數據表後受影響的記錄數
	{
		return mysqli_affected_rows($GLOBALS['link']);
	}
	//執行escape_string()函數
	function EDbEscapeStr($str){
		$str=mysqli_real_escape_string($GLOBALS['link'],$str);
		return $str;
	}
	//取得數據庫版本
	function egetdbver()
	{
		$this->r=$this->fetch1('select version() as version');
		return $this->r['version'];
	}
}
?>