在PHP中,数据库操作是Web开发中不可或缺的一部分。DB类可以封装数据库操作的方法,使得代码更加简洁和易于维护。以下是一个简单的DB类的实例,包括连接数据库、查询和更新数据库的基本操作。
1. DB类的基本结构
```php
class DB {
private $host = 'localhost';
private $username = 'root';
private $password = '';
private $database = 'test';
private $connection;
public function __construct() {
$this->connect();
}
private function connect() {
$this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);
if ($this->connection->connect_error) {
die("