php使用面对对象思想实现数据库简单的增删改查操作

php学习笔记

使用Mysqli实现增删改查

1.连接数据库

	//连接数据库
	//$database: 数据库名
	public function connect($database) {
		$servername = "localhost";
		$username = "root";
		$password = "root";
		$dbname = $database;

		// 创建连接
		$this->conn = new mysqli($servername, $username, $password, $dbname);

		// 检测连接
		if ($this->conn->connect_error) {
			die("连接失败: " . $this->conn->connect_error);
		} 
		// echo '连接成功<br />';
		// return $this->conn;
	}

2.获取一条数据

	//获取一条数据
	//$table: 数据表
	//$where: 要查询的数据, 默认为空
	public function getOne($table, $where = '') {
		if($where != "") {
			$sql = "SELECT * FROM {$table} WHERE {$where}";
		}
		else {
			$sql = "SELECT * FROM {$table}";
		}

		$result = $this->conn->query($sql);

		$data = [];

		$row = mysqli_num_rows($result);

		if ($row > 0) {
			$data = mysqli_fetch_assoc($result);
			// echo '查询成功<br />';
			return $data;
		}
		else {
			// echo '查询失败<br />';
			return false;
		}
	}

3.获取多条数据

	//获取所有数据
	//$table: 数据表
	//$where: 要查询的数据, 默认为空
	public function getAll($table, $where = '') {
		if($where != "") {
			$sql = "SELECT * FROM {$table} WHERE {$where}";
		}
		else {
			$sql = "SELECT * FROM {$table}";
		}

		$result = $this->conn->query($sql);

		$data = [];


		$row = mysqli_num_rows($result);

		if ($row > 0) {
			while($row = mysqli_fetch_assoc($result)) {
				$data[] = $row;
			}
			// echo '查询成功<br />';
			return $data;
		}
		else {
			// echo '查询失败<br />';
			return false;
		}
	}

}

4.插入数据

	//插入数据
	//$table: 数据表
	//$data: 要插入的数据
	public function insert($table, $data) {
		$fields = "";
		$values = "";
		//拼接sql,把data解析成字段和值,可以实现复用
		foreach ($data as $key => $value) {
			
			if ($fields == "") {
				$fields = $key;
			}
			else {
				$fields = $fields.','.$key;
			}

			if ($values == "") {
				$values = "'".$value."'";
			}
			else {
				$values = $values.",'".$value."'";
			}

		}

		//sql语句
		$sql = "INSERT INTO {$table} ({$fields})
		VALUES ({$values})";

		$result = $this->conn->query($sql);
		if ($result == true) {
			// echo '插入成功<br />';
			return true;
		}
		else {
			// echo '插入失败<br />';
			return false;
		}
	}

5.删除数据

	//删除数据
	//$table: 数据表
	//$where: 删除条件
	public function delete($table, $where) {
		$sql = "DELETE FROM {$table} WHERE {$where}";

		$result = $this->conn->query($sql);

		$row = mysqli_affected_rows($this->conn);

		if ($row > 0) {
			//echo '删除成功<br />';
			return true;
		}
		else {
			//echo '删除失败<br />';
			return false;
		}
	}

6.更新数据

	//更新数据
	//$table: 数据表
	//$data: 要更新的数据
	//$where: 更新条件
	public function update($table, $data, $where) {
		$before = "UPDATE {$table} SET ";
		$after = " WHERE {$where}";

		$fields = "";
		foreach ($data as $key => $value) {
			if ($fields == "") {
				$fields = "{$key} = "."'{$value}'";
			}
			else {
				$fields = $fields.",{$key} = "."'{$value}'";
			}
		}

		//sql语句
		$sql = $before.$fields.$after;

		$result = $this->conn->query($sql);

		$row = mysqli_affected_rows($this->conn);

		if ($row > 0) {
			// echo '修改成功<br />';
			return true;
		}
		else {
			// echo '修改失败<br />';
			return false;
		}
	}

第一次发博客,纪念一下。

猜你喜欢

转载自blog.csdn.net/Moriarty123666/article/details/84941433