ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
``` <?php // 定义为开启主从的开关 define('swich',true); class DB { public $pdo; public $readPdo; public function __construct() { // 主数据库(主要是写入) $this->pdo = new PDO('mysql:host=localhost;dbname=lamp129;charset=utf8','root',''); // 从数据库(主要是查询) if ($this->swich) { $this->readPdo = new PDO('mysql:host=192.168.129.251;dbname=lamp129;charset=utf8','root',''); } else { $this->readPdo = $this->pdo; } } // 查询 public function select() { $this->readPdo->query('select * from user'); } // 添加操作 public function insert() { $this->pdo->exec('insert into user(username) values("zhangsan")'); } } ```