php操作mysql类库,包含增删改查等基本操作

上代码

<?php

class MySQL {
    private $host;
    private $username;
    private $password;
    private $database;

    private $conn;

    function __construct($host, $username, $password, $database) {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;

        $this->conn = mysqli_connect($host, $username, $password, $database);
        if(mysqli_connect_errno()) {
            throw new Exception(mysqli_connect_error());
        }
    }

    // 查询操作
    public function query($sql) {
        $result = mysqli_query($this->conn, $sql);
        if(!$result) {
            throw new Exception(mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    // 获取单条记录
    public function getOne($sql) {
        $result = $this->query($sql);
        $row = mysqli_fetch_assoc($result);
        return $row;
    }

    // 获取多条记录
    public function getAll($sql) {
        $result = $this->query($sql);
        $rows = array();
        while($row = mysqli_fetch_assoc($result)) {
            $rows[] = $row;
        }
        return $rows;
    }

    // 插入操作
    public function insert($table, $data) {
        $keys = array();
        $values = array();
        foreach($data as $key => $value) {
            $keys[] = "`{$key}`";
            $values[] = "'{$value}'";
        }
        $keys_str = implode(",", $keys);
        $values_str = implode(",", $values);
        $sql = "INSERT INTO `{$table}` ({$keys_str}) VALUES ({$values_str})";
        $this->query($sql);
        return $this->getInsertId();
    }

    // 获取刚插入的自增主键ID
    public function getInsertId() {
        return mysqli_insert_id($this->conn);
    }

    // 更新操作
    public function update($table, $data, $where = '') {
        $set = array();
        foreach($data as $key => $value) {
            $set[] = "`{$key}`='{$value}'";
        }
        $set_str = implode(",", $set);
        $where_str = empty($where) ? '' : " WHERE {$where}";
        $sql = "UPDATE {$table} SET {$set_str}{$where_str}";
        $this->query($sql);
        return $this->affectedRows();
    }

    // 删除操作
    public function delete($table, $where) {
        $where_str = empty($where) ? '' : " WHERE {$where}";
        $sql = "DELETE FROM {$table}{$where_str}";
        $this->query($sql);
        return $this->affectedRows();
    }

    // 受影响行数
    public function affectedRows() {
        return mysqli_affected_rows($this->conn);
    }

    // 释放连接资源
    public function __destruct() {
        mysqli_close($this->conn);
    }
}


//使用示例

//连接Mysql
$db = new MySQL('localhost', 'username', 'password', 'database');

// 查询操作
$result = $db->query("SELECT * FROM `users`");

// 获取单条记录
$row = $db->getOne("SELECT * FROM `users` WHERE `id`=1");

// 获取多条记录
$rows = $db->getAll("SELECT * FROM `users` WHERE `age`>19");

// 插入操作
$last_insert_id = $db->insert("users", array("name" => "tom", "age" => 20, "gender" => "1"));

// 更新操作
$affected_rows = $db->update("users", array("age" => 22), "`name`='tom'");

// 删除操作
$affected_rows = $db->delete("users", "`age`<19");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五六碗瓶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值