91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

如何通過php mysql類管理數據庫

PHP
小樊
83
2024-09-22 00:17:42
欄目: 云計算

要通過PHP和MySQL類管理數據庫,你可以創建一個PHP類,用于連接、查詢、插入、更新和刪除數據

  1. 創建一個名為DatabaseManager.php的文件,并在其中包含以下內容:
<?php
class DatabaseManager {
    private $host = "localhost";
    private $username = "your_username";
    private $password = "your_password";
    private $database = "your_database";

    public function __construct() {
        $this->connect();
    }

    private function connect() {
        $conn = new mysqli($this->host, $this->username, $this->password, $this->database);

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

        $this->conn = $conn;
    }

    public function query($sql) {
        return $this->conn->query($sql);
    }

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

    public function insert($table, $data) {
        $keys = implode(',', array_keys($data));
        $values = ':' . implode(', :', array_keys($data));

        $sql = "INSERT INTO {$table} ({$keys}) VALUES ({$values})";

        $stmt = $this->conn->prepare($sql);
        foreach ($data as $key => $value) {
            $stmt->bindValue(":{$key}", $value);
        }

        return $stmt->execute();
    }

    public function update($table, $data, $condition) {
        $keys = array_keys($data);
        $keys = ':' . implode(', :', $keys);

        $sql = "UPDATE {$table} SET {$keys} WHERE {$condition}";

        $stmt = $this->conn->prepare($sql);
        foreach ($data as $key => $value) {
            $stmt->bindValue(":{$key}", $value);
        }

        return $stmt->execute();
    }

    public function delete($table, $condition) {
        $sql = "DELETE FROM {$table} WHERE {$condition}";
        return $this->query($sql);
    }

    public function select($table, $condition = [], $order_by = null, $limit = null) {
        $sql = "SELECT * FROM {$table} WHERE 1=1";

        if (!empty($condition)) {
            $sql .= " AND {$condition}";
        }

        if ($order_by) {
            $sql .= " ORDER BY {$order_by}";
        }

        if ($limit) {
            $sql .= " LIMIT {$limit}";
        }

        return $this->query($sql);
    }

    public function close() {
        $this->conn->close();
    }
}
?>
  1. 在你的主文件中(例如index.php),包含DatabaseManager.php文件并使用它:
<?php
require_once 'DatabaseManager.php';

$db = new DatabaseManager();

// 插入數據
$data = [
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'age' => 30
];
$db->insert('users', $data);

// 查詢數據
$results = $db->select('users', ['id' => 1]);
foreach ($results as $row) {
    echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Email: " . $row['email'] . "<br>";
}

// 更新數據
$data = [
    'name' => 'Jane Doe',
    'email' => 'jane@example.com'
];
$condition = ['id' => 1];
$db->update('users', $data, $condition);

// 刪除數據
$condition = ['id' => 1];
$db->delete('users', $condition);

// 關閉數據庫連接
$db->close();
?>

這個DatabaseManager類提供了基本的數據庫操作,你可以根據需要擴展它以滿足你的需求。

0
黄山市| 宝兴县| 谢通门县| 元朗区| 禄丰县| 樟树市| 土默特左旗| 靖边县| 桐乡市| 滕州市| 饶平县| 米林县| 久治县| 武功县| 巴青县| 民勤县| 富源县| 神农架林区| 化州市| 海阳市| 娄烦县| 泰和县| 古浪县| 剑川县| 阜新市| 泸水县| 金湖县| 蒙山县| 松阳县| 应城市| 和平县| 庄浪县| 任丘市| 商河县| 大方县| 穆棱市| 武陟县| 汉阴县| 彭阳县| 若羌县| 女性|