php Mysqli数据库操作类及示例php

/ / 2023-06-22   阅读:2494
php Mysqli数据库操作类及示例...

类:

<?php 
class MysqliDB {
    private $conn;
    private $debug;

    function __construct($host, $user, $pass, $db, $debug=false) {
        $this->conn = new mysqli($host, $user, $pass, $db);

        if ($this->conn->connect_error) {
            die("Connection failed: ".$this->conn->connect_error);
        }
        $this->debug = $debug;
    }

    function __destruct() {
        $this->conn->close();
    }

    function query($query) {
        if ($this->debug === true) {
            echo $query . "<br>";
        }
        $result = $this->conn->query($query);

        if (!$result) {
            die($this->conn->error . '<br> Query: ' .$query);
        }

        return $result;
    }

    function select($table, $fields='*', $where='', $order='', $limit='') {
        $query = "SELECT ".$fields." FROM ".$table;
        if ($where != '') {
            $query .= " WHERE " . $where;
        }
        if ($order != '') {
            $query .= " ORDER BY " . $order;
        }
        if ($limit != '') {
            $query .= " LIMIT " . $limit;
        }
        $result = $this->query($query);
        $data = array();
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
        return $data;
    }

    function insert($table, $data) {
        $fields = "";
        $values = "";
        foreach ($data as $key =>$value) {
            $fields .= "`".$key."`,";
            $values .= "'".$value."',";
        }
        $query = "INSERT INTO ".$table."(".rtrim($fields, ',').") VALUES (".rtrim($values, ',').")";
        $result = $this->query($query);
        return $this->conn->insert_id;
    }

    function update($table, $data, $where='') {
        $str = "";
        foreach ($data as $key => $value) {
            $str .= "`".$key."`='".$value."',";
        }
        $str = rtrim($str, ',');
        $query = "UPDATE ".$table." SET ".$str." WHERE ".$where;
        $result = $this->query($query);
        return true;
    }

    function delete($table, $where='') {
        $query = "DELETE FROM ".$table." WHERE ".$where;
        $result = $this->query($query);
        return true;
    }

    function escape($string) {
        return $this->conn->real_escape_string($string);
    }

    function countRows($table, $where='') {
        $query = "SELECT COUNT(*) FROM ".$table;
        if ($where != '') {
            $query .= " WHERE " . $where;
        }
        $result = $this->query($query);
        $row = $result->fetch_row();
        return $row[0];
    }

    function getFields($table) {
        $query = "DESCRIBE ".$table;
        $result = $this->query($query);
        $fields = array();
        while ($row = $result->fetch_assoc()) {
            $fields[] = $row['Field'];
        }
        return $fields;
    }
}
?>


示例:

<?php
$db = new MysqliDB('localhost', 'user', 'password', 'database');

// 查询
$data = $db->select("users", "*", "id=1");

// 插入
$id = $db->insert("users", array(
    "username" => "admin",
    "password" => "password"
));

// 更新
$result = $db->update("users", array(
    "password" => "new_password"
), "id=1");

// 删除
$result = $db->delete("users", "id=1");

// 限制查询数量
$data = $db->select("users", "*", "", "", "10");

// 获取总记录数
$count = $db->countRows("users");

// 获取表字段
$fields = $db->getFields("users");

$db->close();
?>

我要评论

昵称:
验证码:

最新评论

共0条 共0页 10条/页 首页 上一页 下一页 尾页
意见反馈