PHP增删改查的简便类

浮梦 103 2024-07-17

PHP增删改查的简便类,用于小项目直接使用,不用框架了
Thinkphp咋用就咋用,自己配置账号密码后,require引入就行了
注意!Thinkphp框架是Db::name,这个是$Db->name,OK,不懂就留言嗷

<?php
$dbuser = "数据库用户";
$dbpass = "数据库密码";
$dbname = "数据库名称";
$dbhost = "数据库地址";
class Db {
    private $pdo;
    private $tableName;
    // 表前缀
    private $tablePrefix = '';
    private $whereConditions = [];
    private $whereValues = [];

    public function __construct(PDO $pdo) {
        $this->pdo = $pdo;
    }

    // 设置表名前缀
    public function setTablePrefix($prefix) {
        $this->tablePrefix = $prefix;
        return $this; // 链式调用
    }

    // 设置表名(可选包含前缀)
    public function name($tableName) {
        $this->tableName = $this->tablePrefix . $tableName;
        return $this; // 链式调用
    }

    // 添加where条件
    public function where($column, $value) {
        $this->whereConditions[] = "$column = ?";
        $this->whereValues[] = $value;
        return $this; // 链式调用
    }

    // 重置where条件
    public function resetWhere() {
        $this->whereConditions = [];
        $this->whereValues = [];
        return $this; // 链式调用
    }

    // 执行查询并返回结果
    private function query($sql, $params = []) {
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);
        return $stmt;
    }

    // 查询全部记录
    public function select() {
        $sql = "SELECT * FROM {$this->tableName}";
        if (!empty($this->whereConditions)) {
            $sql .= ' WHERE ' . implode(' AND ', $this->whereConditions);
        }
        $stmt = $this->query($sql, $this->whereValues);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 查询单条记录
    public function find() {
        $sql = "SELECT * FROM {$this->tableName}";
        if (!empty($this->whereConditions)) {
            $sql .= ' WHERE ' . implode(' AND ', $this->whereConditions);
            $sql .= ' LIMIT 1';
        }
        $stmt = $this->query($sql, $this->whereValues);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    // 更新数据
    public function update($data) {
        $where = isset($this->whereConditions) ? ' WHERE ' . implode(' AND ', $this->whereConditions) : '';
        $sets = [];
        foreach ($data as $key => $value) {
            $sets[] = "$key = ?";
        }
        $sql = "UPDATE {$this->tableName} SET " . implode(', ', $sets) . $where;
        $params = array_values($data);
        if (isset($this->whereConditions)) {
            $params = array_merge($params, array_values(array_column($this->whereConditions, 1)));
        }
        $stmt = $this->query($sql, $params);
        return $stmt->rowCount() > 0;
    }

    // 插入数据
    public function insert($data) {
        $columns = implode(', ', array_keys($data));
        $placeholders = ':' . implode(', :', array_keys($data));
        $sql = "INSERT INTO {$this->tableName} ($columns) VALUES ($placeholders)";
        $stmt = $this->pdo->prepare($sql);
        $boundParams = [];
        foreach ($data as $key => $value) {
            $boundParams[":" . $key] = $value;
        }
        $stmt->execute($boundParams);
        return $stmt->rowCount() > 0;
    }

    // 删除数据
    public function delete() {
        $sql = "DELETE FROM {$this->tableName}";
        if (isset($this->whereConditions)) {
            $sql .= ' WHERE ' . implode(' AND ', $this->whereConditions);
        }
        $stmt = $this->query($sql);
        return $stmt->rowCount() > 0;
    }
}
$pdo = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
$Db = new Db($pdo);