自定义php+mysql封装类

By | 2020年6月6日

大家用过thinkphp或者laravel等其他php框架,应该了解这些框架在php里运用mysql处理的方法函数都是封装好的,优点就是使用方便。

而这篇文章介绍的就是我个人封装的一个简单的mysql处理类:

<?php
	class Mysql
	{
		protected $servername = DBSERVER;
		protected $username = DBUSERNAME;
		protected $password = DBPASSWORD;
		protected $database = DBDATABASE;
		protected $conn = null;
		protected $limit = "";
		protected $table = "";
		protected $where = "";
		protected $field="*";
		protected $order="";
		public function __construct($diyserver=[]){
			//连接数据库
			$servername=$this->servername;
			$username=$this->username;
			$password=$this->password;
			$database=$this->database;
			if($diyserver&&is_array($diyserver)){
				if(isset($diyserver['servername'])) $servername=$diyserver['servername'];
				if(isset($diyserver['username'])) $username=$diyserver['username'];
				if(isset($diyserver['password'])) $password=$diyserver['password'];
				if(isset($diyserver['database'])) $database=$diyserver['database'];
			}
			$conn = new mysqli($servername, $username, $password, $database);
			if ($conn->connect_error) {
			    die("数据库连接失败: " . $conn->connect_error);
			}
			$this->conn = $conn;
			//连接数据库
		}

		/**
		 * 初始化参数
		 */
		private function init(){
			$this->limit='';
			$this->table = "";
			$this->where = "";
			$this->field = "*";
			$this->order = "";
		}

		/**
		 * 查询数据limit
		 */
		public function limit($limit=0){
			if(!$limit){
				$limit="";
			}
			$this->limit=$limit;
			return $this;
		}

		/**
		 * 使用数据库中的表
		 */
		public function table($table=""){
			$this->table=$table;
			return $this;
		}

		/**
		 * 数据库查询条件
		 */
		public function where($where=""){
			$this->where=$where;
			return $this;
		}

		/**
		 * 排序查询条件
		 */
		public function order($order=""){
			$this->order=$order;
			return $this;
		}

		/**
		 * 需要的字段
		 */
		public function field($field=""){
			$this->field=$field;
			return $this;
		}

		/**
		 * 查询数据
		 */
		public function select(){
			if($this->where){
				$this->where=" WHERE ".$this->where;
			}
			if($this->limit){
				$this->limit=" LIMIT ".$this->limit;
			}else{
				$this->limit="";
			}
			$sql="SELECT ".$this->field." FROM ".$this->table." ".$this->where." ".$this->order." ".$this->limit;
			$result=$this->conn->query($sql);
			$this->init();
			if ($result&&$result->num_rows > 0){
				$rtRes=[];
				while($row = $result->fetch_assoc()) {
			        $rtRes[]=$row;
			    }
			    return $rtRes;
			}else{
				return [];
			}
		}

		/**
		 * 只查询一条数据
		 */
		public function first(){
			if($this->where){
				$this->where=" WHERE ".$this->where;
			}
			$this->limit=" LIMIT 1";
			$sql="SELECT ".$this->field." FROM ".$this->table." ".$this->where." ".$this->order." ".$this->limit;
			$result=$this->conn->query($sql);
			$this->init();
			if ($result&&$result->num_rows > 0){
			    return $result->fetch_assoc();
			}else{
				return [];
			}
		}

		/**
		 * 更新数据
		 */
		public function update($data){
			if($this->where){
				$this->where=" WHERE ".$this->where;
			}
			$sql="UPDATE ".$this->table." SET $data ".$this->where;
			$result=$this->conn->query($sql);
			return $result;
		}

		/**
		 * 新增数据
		 */
		public function insert($data){
			$sql="INSERT INTO ".$this->table." $data ";
			//echo $sql;exit();
			$result=$this->conn->query($sql);
			return $result;
		}

		/**
		 * 直接执行sql语句
		 */
		public function query($sql){
			$result=$this->conn->query($sql);
			if ($result&&$result->num_rows > 0){
				$rtRes=[];
				while($row = $result->fetch_assoc()) {
			        $rtRes[]=$row;
			    }
			    return $rtRes;
			}else{
				return [];
			}
		}

		public function __destruct(){
			if (!empty($this->conn)&&!$this->conn->connect_error){
				mysqli_close($this->conn);
			}
		}
	}
	function M($config=[]){
		return new Mysql($config);
	}

?>

这只是一个简单的封装类,基础的运用是够了,供大家参考参考,也可以优化优化,大家一起参考学习!

发表评论

电子邮件地址不会被公开。 必填项已用*标注