《PHP连接MySQLI类,已连贯操作形式请求内部方法》要点:
本文介绍了PHP连接MySQLI类,已连贯操作形式请求内部方法,希望对您有用。如果有疑问,可以联系我们。
欢迎参与《PHP连接MySQLI类,已连贯操作形式请求内部方法》讨论,分享您的想法,维易PHP学院为您提供专业教程。
<?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);