<?php
//数据库连接对象 mydb
//数据表操作对象 mytb

class mydb
{	
	public  $conn;
	public  $db;
	public  $mysqli;
	
	function __construct($dbname2="") {
		include('conn_var.php');
		if($dbname2!="")$dbname=$dbname2;
		$this->mysqli=new mysqli($host,$username,$password,$dbname) ;
		/* check connection */
		if ($this->mysqli->connect_errno) {
		   printf("Can't connect $dbname : ", $mysqli->connect_error);
 		   exit();
		}else{
			$this->mysqli->set_charset('utf8');
		}
   }
   function __destruct() {
	  unset($this->db);
	  unset($this->mysqli);

   }

	function query($sqlstr)
	{
		$result=@$this->mysqli->query($sqlstr) or die ($_SESSION['cdebug']?"\n $this->mydbname - Query Error: <br/>   ".mysqli_error($this->mysqli)."<br/>   '".$sqlstr."'" :" error2");
		$errormsg=mysqli_error($this->mysqli);
		if(!$_SESSION['cdebug'] && $errormsg!="") error_log(date('y-m-d H:m:s')."  ".$dbname." - $sqlstr \r\nQuery Error: ".$errormsg."\r\n",3,'errors.log');
		if($errormsg!="") die ($_SESSION['cdebug']?"\n $this->mydbname - Query Error: <br/>   ".mysqli_error()."<br/>   '".$sqlstr."'" :" error2");
		
		
		if($result) return $result; else return false; 
 	}

     //返回所有数据的二维数组
	public function AllRows($sqlstr) {
		$result = $this->mysqli->query($sqlstr);
		$list =array();
/*		while($row =  mysqli_fetch_array($result,MYSQLI_NUM)) {	$list[]= $row ;	} */
		while($row = mysqli_fetch_assoc($result))
		{	$list[]= $row ; } //array_push($list, $row);
		return $list;
	} 
	// 返回查询的一行数据，一维数组
	public function OneRow($sqlstr) {
		$result = $this->mysqli->query($sqlstr);
		$one = $result->fetch_row();
		return $one;
	}

	function close()
	{
		$this->mysqli->close();
	}
	
	function result($result,$row=0)  //返回最后相关行第一个字段的值
	{
		mysqli_data_seek($result,$row);
    	$row=mysqli_fetch_row($result);
    	return $row[0] ;
  	}

	function affected_id()			//返回操作影响的最后一行的ID
  	{
   		return  $this->mysqli->affected_rows;
 	}

	function affectedrows()			//返回操作影响的行数
  	{
   		return  $this->mysqli->affected_rows;
 	}

	function insert_id()			//兼容 返回操作影响的最后一行的ID $mysqli->affected_rows
  	{
   		return  $this->mysqli->insert_id;
 	}

};


/*
	表操作类：上移，下移，允许，禁止，删除，
	$opertb->table="ls_download";
	$opertb->id=$id;
	switch ($ls)
	{
		case "up":
			$ls_msg=$opertb->up($nowNum);
			break;

		case "down":
			$ls_msg=$opertb->down($nowNum);
			break;
			
	 	case "unenable":
			$ls_msg=$opertb->unenable();
			break; 
			
		case "enable":
			$ls_msg=$opertb->enable();
			break;
			
		case "del":
			$ls_msg=$opertb->delwfile($downloadFolders,"","downloadName");
			break;

	}
*/
class mytb
{
	public $table;		//当前表
	public $id;			//当前id
	public $opendb;		//打开数据库
	public $ls_msg="";  //反馈信息
	private $delFile=""; //删除的文件
	
   function __construct() {
		
		$this->opendb=new mydb();
   }

   function __destruct() {
   }
	//上移
	// $nowNum  当前序号
	public function up($nowNum){
	$down=$nowNum+1;
	$down_sql="update ".$this->table." set Num=".$nowNum." where Num=".$down." ";
	$down_sql2="update ".$this->table." set Num=Num+1 where id=".$this->id." ";
	//echo $down_sql;
	$down_query=$this->opendb->query($down_sql);
	$down_query=$this->opendb->query($down_sql2);
	$this->ls_msg="上移操作成功！";
	return $this->ls_msg;
	}
	
	//下移
	// $nowNum  当前序号
	public function down($nowNum){
	$up=$nowNum-1;
	$up_sql="update ".$this->table." set Num=".$nowNum." where Num=".$up."  ";
	$up_sql2="update ".$this->table." set Num=Num-1 where id=".$this->id." ";
	
	//echo $up_sql;
	$up_query=$this->opendb->query($up_sql);
	$up_query=$this->opendb->query($up_sql2);
  
	$this->ls_msg="下移操作成功！";
	return $this->ls_msg;
  
	}
  
	//禁止
	public function unenable(){
	$unenable_sql="update ".$this->table." set enable=0 where id=".$this->id." ";
	//echo $unenable_sql;
	$unenable_query=$this->opendb->query($unenable_sql);
	$this->ls_msg="禁止操作成功！";
	return $this->ls_msg;
  
	}
	
	//允许
	public function enable(){
	$enable_sql="update ".$this->table." set enable=1 where id=".$this->id." ";
	//echo $enable_sql;
	$enable_query=$this->opendb->query($enable_sql);
	$this->ls_msg="允许操作成功！";
	return $this->ls_msg;
  
	}

	//删除记录和文件
	public function delwfile($delFolder1,$delFolder2,$delFieldName){
	
	$this->del($delFieldName);  //删除记录
	$path="../".$delFolder1."/".$this->delFile;
	unlink($path);	
	//删除文件
	if($delFolder2!="")
	{
		$path="../".$delFolder2."/".$this->delFile;
		unlink($path);	
	}
	//删除文件
	
	return $this->ls_msg;
	}

	//删除记录
	//$delField  文档的字段名，配合 delwfile
	public function del($delFieldName){
	$sel_sql="select * from ".$this->table." where ID=".$this->id."";
	$sel_query=$this->opendb->query($sel_sql);
	$sel_rows=mysqli_fetch_assoc($sel_query);
	$num_sel="update ".$this->table." set Num=Num-1 where Num>".$sel_rows['Num']." ";//重新排序
	$down_query=$this->opendb->query($num_sel);
	 
	$this->delFile=$sel_rows[$delFieldName];
  
	$del_sql="delete from ".$this->table." where ID=".$this->id."";
	//echo $del_sql;
	$del_query=$this->opendb->query($del_sql);  		//删除记录
	
	$this->ls_msg="删除操作成功！";
	writelog($_SERVER['PHP_SELF'],"delete ".$this->table."  ","ID: ".$this->id);
	
	return $this->ls_msg;
  
	}

};

/*
	cktb 检查数据库中表和字段并更新
	$opertb->table="ls_download";
	$opertb->id=$id;
*/
class cktb
{
	public $table;		//当前表
	public $opendb;		//打开数据库
	public $ls_msg="";  //反馈信息
	
   function __construct() {
		
		$this->opendb=new mydb();
   }

   function __destruct() {
   }

	//检查数据表是否存在，否则增加
	public function checktable($tableName,$addTBSQL){
			
		$sql="show tables like '".$tableName."' ";
		$query=$this->opendb->query($sql);
		if(mysqli_num_rows($query)==0) 
		{				
			$query=$this->opendb->query($addTBSQL);
			if($query)
			{
				$this->ls_msg="表 ".$tableName." 已添加";
				return true;
			}
			else
			{return false;}

		}
			
	}
	
	// 根据sql语句操作
	public function qureytable($qureyTBSQL){
			
		$query=$this->opendb->query($qureyTBSQL);
		if($query)
		{
			$this->ls_msg="查询已执行";
			return true;
		}
		else
		{return false;}
	}
		
	//检查字段是否存在，否则增加
	public function checkfield($tableName,$fieldName,$addFDSQL)
	{
		  
	  if(!$this->issetfield($tableName,$fieldName))
	  {
		  $query=$this->opendb->query($addFDSQL);
		  
		  if($query)
		  {
			  $this->ls_msg="表 ".$tableName." 的 字段 ".$fieldName." 已添加";
			  return true;
		  }
		  else
		  {return false;}

	  }
	}
	

	//字段是否在表里面
	public function issetfield($tableName,$fieldName)
	{	//
	
  		$sql   = " describe  ".$tableName." ".$fieldName; 
		$query   =  $this->opendb->query($sql); 
		if(mysqli_num_rows($query)==1) 
		{	return true; }
		else 
		{	return false; }
	
	}	
	
	public function valueidfield($tableName,$fieldName,$id)
	{	// 返回值 或者 false
	
  		$sql   = " select $fieldName from   `".$tableName."` where ID=$id"; 
		$query   =  $this->opendb->query($sql); 
		$rows=mysqli_fetch_assoc($query);
		if($query) 
		{	return $rows[$fieldName]; }
		else 
		{	return false; }
	
	}	
	
 
	public function setvalueidfield($tableName,$fieldName,$id,$value=1)
	{	// 返回值 或者 false
	
  		$sql   = " update   `".$tableName."` set  $fieldName = ".$value." where ID=$id"; 
		$query   =  $this->opendb->query($sql); 
		if($query) 
		{	return true; }
		else 
		{	return false; }
	
	}	

};

/*
	mytips 查询ls_tips表提示内容和提示内容更新
	$mytips->table="ls_download";
	$mytips->id=$id;
*/
class mytips
{
	public $table;		//当前表
	public $ls_msg="";  //反馈信息
	
   function __construct() {
		
		$this->opendb=new mydb();
   }

   function __destruct() {
   }

	//  
	public function gettips($tipstitle,$title="text"){
			$qureyTBSQL="select * from ls_tips where tablename='".$this->table."' and title='".$tipstitle."' ";
			$query=$this->opendb->query($qureyTBSQL);
			$select_rows2=mysqli_num_rows($query); //总数

			if($select_rows2==0)
			{
				$this->ls_msg=$tipstitle."没有提示";
				return false;
			}
			else
			{
				$tips=mysqli_fetch_assoc($query);
				if($title=="text")return htmlspecialchars_decode($tips['text']);
				if($title=="pic")return $tips['pic'];
				if($title=="video")return $tips['video'];
			}
	}
	
	// 
	public function puttips($tipstitle,$tipstext){
			$qureyTBSQL="update   ls_tips set text='".htmlspecialchars($tipstext)."' where tablename='".$this->table."' and title='".$tipstitle."' ";
			$query=$this->opendb->query($qureyTBSQL);
			if($query)
			{
				$this->ls_msg=$tipstitle."提示已更新";
				return true;
			}
			else
			{return false;}
	}
				
};



//mysql 与 mysqli 转换函数
if(!function_exists('mysqli_connect')){
	
}

?>