<?PHP
/**
* Created by PHPStorm.
* User: 宇晨PHP培训 www.hnPHP.net
* Date: 2017/4/20
* Time: 9:06
*/
class MysqL
{
private static $Obj;//作为实例化对象的存储
private static $DB;//定义一个私有的静态属性存放MysqL对象
private $sqlArr=array();//定义处理之后sql语句代码片段
public $TableName='';//定义当前模型所使用的表名
private function __construct()//把构造方法设置为私有的禁止外部实例化调用
{
self::$DB=new MysqLi(DBHOST,DBUSER,DBPWD,DBNAME,DBPORT);
self::$DB->set_charset(DBCODE);
if(self::$DB->connect_errno>0)
{
echo '<hr>错误连接编码:'.self::$DB->connect_errno.'<br>';
echo '错误连接信息:'.self::$DB->connect_error.'<hr>';
exit();
}
$this->clearData();//清除sql语句片段
}
//清除sql语句片段
private function clearData()
{
$this->sqlArr['Fields']='*';
$this->sqlArr['Where']='';
$this->sqlArr['Order']='';
$this->sqlArr['Limit']='';
$this->sqlArr['Data']='';
$this->sqlArr['Join']='';
}
private function __clone()//把克隆方法设置为私有的禁止外部克隆当前对象
{
return false;
}
public static function CallRun()//调用运行当前类
{
if(!(self::$Obj instanceof self))
{
self::$Obj=new self;
}
return self::$Obj;
}
//定义sql语句的表名
public function table($Param='')
{
$this->TableName=$Param;
return self::$Obj;
}
//定义sql语句的具体字段
public function fields($Param='')
{
$this->sqlArr['Fields']=empty($Param)?'*':$Param;
return self::$Obj;
}
//定义sql语句where条件的片段语句
public function where($Param=array())
{
$Str=' where 1 ';
if(!empty($Param) && is_array($Param))
{
foreach($Param as $key=>$val)
{
$Type=empty($val[0])?'=':$val[0].' ';
$Value=$val[1]==''||$val[1]===false?'':$val[1].' ';
$Mark=empty($val[2])?' And ':$val[2].' ';
$Str.=$Mark.$key.' '.$Type.' '.$Value;
}
}
$this->sqlArr['Where']=$Str;
return self::$Obj;
}
//定义sql语句 join 代码片段
public function join($Param='')
{
$this->sqlArr['Join']=$Param;
return self::$Obj;
}
//定义sql语句 Order By 排序的节点代码片段
public function order($Param='')
{
$Str='';
if(!empty($Param))
{
$Str=' order by '.$Param;
}
$this->sqlArr['Order']=$Str;
return self::$Obj;
}
//定义sql语句 limit 代码片段
public function limit($Param='')
{
$Str='';
if(!empty($Param))
{
$Str=' limit '.$Param;
}
$this->sqlArr['Limit']=$Str;
return self::$Obj;
}
//作为sql语句的末级节点,把所有拼接起来的sql语句片段进行整合对接 单条 select 语句获取形式
public function find()
{
$Str=$this->sqlArr['Limit'];
if(empty($this->sqlArr['Limit']))
{
$Str=' Limit 1';
}
$Result=$this->sqlQuery($Str);
return $Result->fetch_assoc();
}
//获取多条数据
public function select()
{
$Str='';
if(!empty($this->sqlArr['Limit']))
{
$Str=$this->sqlArr['Limit'];
}
$Result=$this->sqlQuery($Str);
$List=array();
while($Row=$Result->fetch_assoc())
{
$List[]=$Row;
}
return $List;
}
private function sqlQuery($Str='')
{
$sql='select '.$this->sqlArr['Fields'].' from '.DBPREFIX.$this->TableName.$this->sqlArr['Join'].$this->sqlArr['Where'].$this->sqlArr['Order'].$Str;
//echo $sql.'<hr>';
return $this->query($sql);
}
//处理添加或更新时的key value对数据
public function data($Param=array())
{
$Arr=array();
if(!empty($Param) && is_array($Param))
{
foreach($Param as $key => $val)
{
$Arr['Key'][]=trim($key);
$Arr['Val'][]='"'.trim($val).'"';
}
}
$this->sqlArr['Data']=$Arr;
return self::$Obj;
}
//数据添加
public function insert()
{
if(empty($this->sqlArr['Data']) && !is_array($this->sqlArr['Data']))
{
return false;
}
$KeyStr=implode(',',$this->sqlArr['Data']['Key']);
$ValStr=implode(',$this->sqlArr['Data']['Val']);
$sql='insert into '.DBPREFIX.$this->TableName.' ('.$KeyStr.') values ('.$ValStr.')';
return $this->query($sql);
}
//获取添加数据的ID
public function insertid()
{
return self::$DB->insert_id;
}
//更新数据
public function update()
{
if(empty($this->sqlArr['Data']) && !is_array($this->sqlArr['Data']))
{
return false;
}
$Arr='';
foreach($this->sqlArr['Data']['Key'] as $key=>$val)
{
$Arr[]=$val.'='.$this->sqlArr['Data']['Val'][$key];
}
$Str=implode(',$Arr);
$sql='update '.DBPREFIX.$this->TableName.' set '.$Str.$this->sqlArr['Where'].$this->sqlArr['Limit'];
//echo $sql;
return $this->query($sql);
}
//删除数据
public function delete()
{
if(empty($this->sqlArr['Limit']))
{
$this->sqlArr['Limit']=' Limit 1';
}
$sql='delete from '.DBPREFIX.$this->TableName.$this->sqlArr['Where'].$this->sqlArr['Limit'];
//echo $sql;
return $this->query($sql);
}
public function query($sql)
{
$this->clearData();//清除sql语句片段
$Result=self::$DB->query($sql);
if(self::$DB->errno>0)
{
echo '<hr>sql语句错误编号:'.self::$DB->errno.'<br>';
echo 'sql语句错误描述:'.self::$DB->error.'<hr>';
}
return $Result;
}
public function __destruct()
{
self::$DB->close();
}
}
//$Obj1=MysqL::CallRun();
//$Obj2=MysqL::CallRun();
//echo $Obj1->insertid();
//var_dump($Obj1);
//var_dump($Obj2);