PHP自写的一个MYSQL操作类

在此做个记录

PHP类的几个知识点

self::$静态属性名

$this->一般属性名或方法名


单例类 四个要点(三私一公)

1禁止构造对象---禁止new()  private  __construct(){} 私有空定义

2禁止克隆对象-- clone       private __clone(){} 私有空定义

3定义单例对象属性  private static $对象名   私有静态空定义

4定义入口方法 public static function 方法名() 公共静态


面向对向


'127.0.0.1',
		'port'       =>'3306',
		'username'   =>'root',
		'password'   =>'',
		'dbname'     =>'mysql',
		'charset'    =>'utf8',
	);
	
	//*********************************
	//定义单例(三私一公)
	//私有化静态对象
	private static $db;

	//私有化构造:禁止构造---new()
	private function __construct(){}

	//私有化克隆:禁止克隆
	private function __clone(){}

	//单例对象入口
	public static function getdb($arr=null){
		//如果不存在对象,则建立对象
		if(!isset(self::$db)){self::$db=new self();}

		//如果有连接参数
		if($arr){
			if(isset($arr['host']))self::$db->cs['host']=$arr['host'];
			if(isset($arr['port']))self::$db->cs['port']=$arr['port'];
			if(isset($arr['username']))self::$db->cs['username']=$arr['username'];
			if(isset($arr['password']))self::$db->cs['password']=$arr['password'];
			if(isset($arr['dbname']))self::$db->cs['dbname']=$arr['dbname'];
			if(isset($arr['charset']))self::$db->cs['charset']=$arr['charset'];	
		}

		return self::$db;
	}

	//*****************************************
	//连接成员方法
	private function connect(){
		//$this->db = @ new mysqli(
		$this->mysqli = @ new mysqli(
			$this->cs["host"].":".$this->cs["port"], 
			$this->cs["username"],
			$this->cs["password"]
			);

		/* 检查连接 */
		if (mysqli_connect_errno()) {
		   echo "
*************************************"; echo "
连接失败!!!"; echo "
错误代码:".mysqli_connect_errno(); echo "
错误信息:".mysqli_connect_error(); echo "
*************************************"; exit("
stop!!!"); } } //设置编码 private function charset($char="utf8"){ $this->mysqli->query("set names $char"); } //打开数据库 private function usedb($dbname="mysql"){ $this->mysqli->query("use $dbname"); } //数据库准备 private function ready(){ $this->charset($this->cs['charset']); $this->usedb($this->cs['dbname']); } //执行SQL命令 //$sql---命令:string //$erro----报错开关:任意不是NULL的值0123.... private function query($sql,$erro=null){ $this->connect(); $this->ready(); $this->result = $this->mysqli->query($sql); if($erro!==null){ if(!$this->result){ echo "
****************************"; echo "
执行失败"; echo "
命令:".$sql; echo "
错误代码:".$this->mysqli->errno; echo "
错误信息:".$this->mysqli->error; echo "
****************************"; die("
STOP!!!"); } } return $this->result; } //公开的有返回的命令执行 //返回结果保存在$this-arr //结果类型 array true/false 三种 //参数: //$sql---MYSQL命令 //$index---返回数据的下标类型,默认为字段下标 // 类型: 'n'---数值下标 // 'a'---字段下标 // 'b'---数值/字段(两种)双下标 public function queryarr($sql=null,$index='a'){ if($sql===null){return;} //$r=$this->query($sql,0);//加第二个参数打开报错 $res=$this->query($sql); //结果对象只有三种情况 //1.正确执行,但没有返回结果集.---布尔值:true //2.执行失败---布尔值:false //3.正确执行,有返回结果,返回一个对象----对象:object //可以从对象中取出结果存入本地的数组 //注意一种情况,就是返回空数组.array{} if($res===true){ $this->arr=true; }else{ if($res===false){ $this->arr=false; }else{ //取得全部数据 三类下标 switch ($index) { case 'a': $this->arr=$res->fetch_all(MYSQLI_ASSOC);//字段下标 break; case 'n': $this->arr=$res->fetch_all(MYSQLI_NUM);//数值下标 break; case 'b': $this->arr=$res->fetch_all(MYSQLI_BOTH);//双下标 break; } $res->free();//释放结果对象 } } $this->mysqli->close();//关闭连接对象 $this->mysqli=null;//复位 $this->result=null;//复位 return $this->arr; } //********************** //纯输出数据到表格table public function tab($arr) { if(gettype($arr)!='array'){ if($arr===true){ $arr=array(array('true')); }else{ $arr=array(array('false')); } } if($arr==null)$arr=array(array("null")); echo ""; foreach($arr as $k1=>$v1) { //********************** //表头 if($k1==0) { echo ""; foreach($v1 as $k2=>$v2) { echo ""; } echo ""; } //********************** //内容 echo ""; foreach($v1 as $k2=>$v2) { echo ""; } echo ""; } echo "
"; echo $k2; echo "
"; echo $v2; echo "
"; } //SQL字符转义 public function escape($str){ $this->connect(); $s=$this->mysqli->escape_string($str); $this->mysqli->close();//断开数据库服务器的连接 $this->mysqli=null; return $s; } //测试成员方法 public function test(){ /* $a=123; $b=$this->escape($a); echo "
asdg asdf $b"; */ //$sql="select session_content from session where session_id='$sess_id'"; //增 //$sql="replace into session values('abcd','cccc',unix_timestamp())"; //$sql="replace into session values('aaaa','cccc',unix_timestamp())"; //$sql="insert into session values('abcd11','cccc1',unix_timestamp())"; //$sql="select * from session where session_id='abcd'"; //$sql="select * from session where session_id='abcd222'"; //$sql="set a=1"; //$sql="select * from session "; //$sql="select * from session where session_id='777'"; //$this->queryarr($sql); //$this->connect(); //$this->ready(); //echo "
".$this->cs['charset']; //echo "
";
		//var_dump($this->arr);
		//echo "
"; //$this->tab($this->arr); } } $cs=array( 'host' =>'127.0.0.1', 'port' =>'3306', 'username' =>'xxxx', 'password' =>'1234abcd', 'dbname' =>'xxxx', 'charset' =>'utf8', ); $op1=mysqli_object_db::getdb($cs); echo "
";
//var_dump($op1);
echo "
"; $op1->test();

面向过程

'127.0.0.1',
		'port'       =>'3306',
		'username'   =>'root',
		'password'   =>'',
		'dbname'     =>'mysql',
		'charset'    =>'utf8',
	);
	
	//*********************************
	//定义单例(三私一公)
	//私有化静态对象
	private static $db;

	//私有化构造:禁止构造---new()
	private function __construct(){}

	//私有化克隆:禁止克隆
	private function __clone(){}

	//单例对象入口
	public static function getdb($arr=null){
		//如果不存在对象,则建立对象
		if(!isset(self::$db)){self::$db=new self();}

		//如果有连接参数
		if($arr){
			if(isset($arr['host']))self::$db->cs['host']=$arr['host'];
			if(isset($arr['port']))self::$db->cs['port']=$arr['port'];
			if(isset($arr['username']))self::$db->cs['username']=$arr['username'];
			if(isset($arr['password']))self::$db->cs['password']=$arr['password'];
			if(isset($arr['dbname']))self::$db->cs['dbname']=$arr['dbname'];
			if(isset($arr['charset']))self::$db->cs['charset']=$arr['charset'];	
		}
		return self::$db;
	}
	//*****************************************
	//连接成员方法
	private function connect(){
		//$this->db = @ new mysql(
		$this->link = @mysqli_connect(
			$this->cs["host"].":".$this->cs["port"], 
			$this->cs["username"],
			$this->cs["password"]
			);

		/* 检查连接 */
		if (mysqli_connect_errno()) {
		   echo "
*************************************"; echo "
连接失败!!!"; echo "
错误代码:".mysqli_connect_errno(); echo "
错误信息:".mysqli_connect_error(); echo "
*************************************"; exit("
stop!!!"); } } //设置编码 private function charset($char="utf8"){ mysqli_query($this->link,"set names $char"); } //打开数据库 private function usedb($dbname="mysql"){ mysqli_query($this->link,"use $dbname"); } //数据库准备 private function ready(){ $this->charset($this->cs['charset']); $this->usedb($this->cs['dbname']); } //执行SQL命令 //$sql---命令:string //$erro----报错开关:任意不是NULL的值0123.... private function query($sql,$erro=null){ $this->connect(); $this->ready(); $this->result =mysqli_query($this->link,$sql); if($erro!==null){ if(!$this->result){ echo "
****************************"; echo "
执行失败"; echo "
命令:".$sql; echo "
错误代码:".mysqli_errno($this->link); echo "
错误信息:".mysqli_error($this->link); echo "
****************************"; die("
STOP!!!"); } } return $this->result; } //公开的有返回的命令执行 //返回结果保存在$this-arr //结果类型 array true/false 三种 //参数: //$sql---MYSQL命令 //$index---返回数据的下标类型,默认为字段下标 // 类型: 'n'---数值下标 // 'a'---字段下标 // 'b'---数值/字段(两种)双下标 public function queryarr($sql=null,$index='a'){ if($sql===null){return;} //$res=$this->query($sql,0);//加第二个参数打开报错 $res=$this->query($sql); //结果对象只有三种情况 //1.正确执行,但没有返回结果集.---布尔值:true //2.执行失败---布尔值:false //3.正确执行,有返回结果,返回一个对象----对象:object //可以从对象中取出结果存入本地的数据 if($res===true){ $this->arr=true; }else{ if($res===false){ $this->arr=false; }else{ //取得数组全部 //MYSQLI_ASSOC/字段下标, MYSQLI_NUM/数字下标, MYSQLI_BOTH/字段和数字双下标 switch ($index) { case 'a'://字段下标 $this->arr=mysqli_fetch_all($res,MYSQLI_ASSOC); break; case 'n'://数值下标 $this->arr=mysqli_fetch_all($res,MYSQLI_NUM); break; case 'b': $this->arr=mysqli_fetch_all($res,MYSQLI_BOTH); break; } mysqli_free_result ($res);//释放数据库服务器的结果资源 } } mysqli_close ($this->link);//断开数据库服务器的连接 $this->link=null;//复位 $this->result=null;//复位 return $this->arr; } //********************** //纯输出数据到表格table public function tab($arr) { if(gettype($arr)!='array'){ if($arr===true){ $arr=array(array('true')); }else{ $arr=array(array('false')); } } if($arr==null)$arr=array(array("null")); echo ""; foreach($arr as $k1=>$v1) { //********************** //表头 if($k1==0) { echo ""; foreach($v1 as $k2=>$v2) { echo ""; } echo ""; } //********************** //内容 echo ""; foreach($v1 as $k2=>$v2) { echo ""; } echo ""; } echo "
"; echo $k2; echo "
"; echo $v2; echo "
"; } public function escape($str){ $this->connect(); $s=mysqli_escape_string($this->link,$str); mysqli_close ($this->link);//断开数据库服务器的连接 $this->link=null; return $s; } //测试成员方法 public function test(){ /* $a=123; $this->connect(); $b=mysqli_escape_string($this->link,$a); mysqli_close ($this->link);//断开数据库服务器的连接 $this->link=null; $c="asdf asd $b"; echo $c; */ //$sql="select session_content from session where session_id='$sess_id'"; //增 //$sql="replace into session values('abcd','cccc',unix_timestamp())"; //$sql="replace into session values('aaaa','cccc',unix_timestamp())"; //$sql="insert into session values('abcd11','cccc1',unix_timestamp())"; //$sql="select * from session where session_id='abcd'"; //$sql="select * from session where session_id='abcd222'"; //$sql="set a=1"; $sql="select * from session "; //$sql="select * from session where session_id='777'"; //$sql="select session_content from session where session_id='abcd'"; $this->queryarr($sql); //$this->connect(); //$this->ready(); //echo "
".$this->cs['charset']; //echo "
";
		//var_dump($this->arr);
		//echo "
"; $this->tab($this->arr); } }

 
  


你可能感兴趣的:(MYSQL,PHP)