ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 主从分布 ``` <pre class="calibre29">``` <span class="token1"><</span><span class="token1">?</span>php <span class="token">// 定义为开启主从的开关</span> <span class="token4">define</span><span class="token3">(</span><span class="token2">'swich'</span><span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">;</span> class <span class="token4">DB</span> <span class="token3">{</span> public $pdo<span class="token3">;</span> public $readPdo<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 主数据库(主要是写入)</span> $this<span class="token1">-</span><span class="token1">></span>pdo <span class="token1">=</span> <span class="token5">new</span> <span class="token4">PDO</span><span class="token3">(</span><span class="token2">'mysql:host=localhost;dbname=lamp129;charset=utf8'</span><span class="token3">,</span><span class="token2">'root'</span><span class="token3">,</span><span class="token2">''</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 从数据库(主要是查询)</span> <span class="token5">if</span> <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>swich<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>readPdo <span class="token1">=</span> <span class="token5">new</span> <span class="token4">PDO</span><span class="token3">(</span><span class="token2">'mysql:host=192.168.129.251;dbname=lamp129;charset=utf8'</span><span class="token3">,</span><span class="token2">'root'</span><span class="token3">,</span><span class="token2">''</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>readPdo <span class="token1">=</span> $this<span class="token1">-</span><span class="token1">></span>pdo<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">// 查询</span> public <span class="token5">function</span> <span class="token4">select</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>readPdo<span class="token1">-</span><span class="token1">></span><span class="token4">query</span><span class="token3">(</span><span class="token2">'select * from user'</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">// 添加操作</span> public <span class="token5">function</span> <span class="token4">insert</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>pdo<span class="token1">-</span><span class="token1">></span><span class="token4">exec</span><span class="token3">(</span><span class="token2">'insert into user(username) values("zhangsan")'</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> ``` ```