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;
		}
	}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值