手写php7.0到php8.0可以使用mysql数据库增删改查一个类库
class Database { private $host; private $user; private $pass; private $dbname; private $charset; private $pdo; public function __construct($host, $user, $pass, $dbname, $charset='utf8mb4') { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->dbname = $dbname; $this->charset = $charset; $this->connect(); } private function connect() { $dsn = "mysql:host={$this->host};dbname={$this->dbname};charset={$this->charset}"; $this->pdo = new PDO($dsn, $this->user, $this->pass); $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } public function query($sql, $params = []) { $stmt = $this->pdo->prepare($sql); $stmt->execute($params); return $stmt; } public function fetchAll($sql, $params = []) { $stmt = $this->query($sql, $params); return $stmt->fetchAll(); } public function fetch($sql, $params = []) { $stmt = $this->query($sql, $params); return $stmt->fetch(); } public function insert($table, $data) { $keys = array_keys($data); $fields = implode(",", $keys); $values = ":" . implode(",:", $keys); $sql = "INSERT INTO $table ($fields) VALUES ($values)"; $this->query($sql, $data); return $this->pdo->lastInsertId(); } public function update($table, $data, $where) { $set = []; foreach($data as $key => $value) { $set[] = "$key = :$key"; } $set = implode(",", $set); $sql = "UPDATE $table SET $set WHERE $where"; return $this->query($sql, $data); } public function delete($table, $where) { $sql = "DELETE FROM $table WHERE $where"; return $this->query($sql); } }
可以使用PHP Data Object (PDO)来连接和操作MySQL数据库。以下是一个基本的类库,可用于执行增删改查操作。
$db = new Database('localhost', 'username', 'password', 'database'); $rows = $db->fetchAll("SELECT * FROM users"); foreach ($rows as $row) { echo $row['name']; }
此类库使用PDO来连接和操作MySQL数据库,因此您需要启用PDO扩展模块以使用此库。
下面是基于 mysqli 扩展实现的简单数据库类库,可以进行增删改查操作
class MySQLiDB { private $host; private $user; private $password; private $dbname; private $charset; private $conn; // 构造函数 public function __construct($host, $user, $password, $dbname, $charset = 'utf8') { $this->host = $host; $this->user = $user; $this->password = $password; $this->dbname = $dbname; $this->charset = $charset; // 连接数据库 $this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->dbname); if (!$this->conn) { die('连接失败:' . mysqli_connect_error()); } // 设置字符集 mysqli_set_charset($this->conn, $this->charset); } // 执行 SQL 语句 private function query($sql) { $result = mysqli_query($this->conn, $sql); if (!$result) { die('执行 SQL 语句失败:' . mysqli_error($this->conn)); } return $result; } // 查询多行数据 public function select($sql) { $result = $this->query($sql); $rows = array(); while ($row = mysqli_fetch_assoc($result)) { $rows[] = $row; } return $rows; } // 查询一行数据 public function find($sql) { $result = $this->query($sql); $row = mysqli_fetch_assoc($result); return $row; } // 插入数据 public function insert($table, $data) { $keys = array(); $values = array(); foreach ($data as $key => $value) { $keys[] = "`" . mysqli_real_escape_string($this->conn, $key) . "`"; $values[] = "'" . mysqli_real_escape_string($this->conn, $value) . "'"; } $key_str = implode(',', $keys); $value_str = implode(',', $values); $sql = "INSERT INTO `$table` ($key_str) VALUES ($value_str)"; $this->query($sql); return mysqli_insert_id($this->conn); } // 更新数据 public function update($table, $data, $condition) { $set = array(); foreach ($data as $key => $value) { $set[] = "`" . mysqli_real_escape_string($this->conn, $key) . "`='" . mysqli_real_escape_string($this->conn, $value) . "'"; } $set_str = implode(',', $set); $sql = "UPDATE `$table` SET $set_str WHERE $condition"; $this->query($sql); return mysqli_affected_rows($this->conn); } // 删除数据 public function delete($table, $condition) { $sql = "DELETE FROM `$table` WHERE $condition"; $this->query($sql); return mysqli_affected_rows($this->conn); } // 析构函数 public function __destruct() { mysqli_close($this->conn); } }
使用方法如下:
//实例化类并连接数据库 $db = new MySQLiDB('localhost', 'username', 'password', 'database_name'); //查询多行数据 $sql = "SELECT * FROM `table_name` WHERE `id` > 1"; $result = $db->select($sql); //查询一行数据 $sql = "SELECT * FROM `table_name` WHERE `id` = 1"; $result = $db->find($sql); //插入数据 $data = array( 'name' => 'blackcatw', 'age' => 25, 'email' => '[email protected]' ); $table = 'table_name'; $db->insert($table, $data); //更新数据 $data = array( 'name' => 'John Doe', 'age' => 30 ); $condition = "`id` = 1"; $table = 'table_name'; $db->update($table, $data, $condition); //删除数据 $condition = "`id` = 1"; $table = 'table_name'; $db->delete($table, $condition);
注意:在执行 SQL 语句时,应该确保 SQL 语句中的变量已经进行了安全转义,以避免 SQL 注入攻击。这里的类库使用了 mysqli_real_escape_string
函数来进行安全转义。