ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 数据库操作 - 新增db.class.php调用 使用全部方法,需要以对象方式调用 <https://www.kancloud.cn/logoove/we7/678561> ``` <pre class="calibre14">``` <span class="token1">require_once</span><span class="token2">(</span>DEDEINC<span class="token2">.</span><span class="token4">'/Db.class.php'</span><span class="token2">)</span><span class="token2">;</span> $db <span class="token">=</span> <span class="token5">new</span> <span class="token1">Db</span><span class="token2">(</span>$GLOBALS<span class="token2">[</span><span class="token4">'cfg_dbhost'</span><span class="token2">]</span><span class="token2">,</span>$GLOBALS<span class="token2">[</span><span class="token4">'cfg_dbuser'</span><span class="token2">]</span><span class="token2">,</span>$GLOBALS<span class="token2">[</span><span class="token4">'cfg_dbpwd'</span><span class="token2">]</span><span class="token2">,</span><span class="token4">'3306'</span><span class="token2">,</span>$GLOBALS<span class="token2">[</span><span class="token4">'cfg_dbname'</span><span class="token2">]</span><span class="token2">,</span>$GLOBALS<span class="token2">[</span><span class="token4">'cfg_dbprefix'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 单条数据 ``` <pre class="calibre14">``` global $dsql<span class="token2">;</span> $row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetOne</span><span class="token2">(</span><span class="token4">"SELECT * FROM hx_arctype WHERE id= $id"</span><span class="token2">)</span><span class="token2">;</span> 新写法 global $db<span class="token2">;</span> $row <span class="token">=</span>$db<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'arctype'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span>$id<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 多条数据 ``` <pre class="calibre14">``` $sql <span class="token">=</span> <span class="token4">"Select * from #@__arctype"</span><span class="token2">;</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">SetQuery</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">Execute</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">while</span><span class="token2">(</span>$row <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetArray</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span> $row1<span class="token2">[</span><span class="token2">]</span><span class="token">=</span> <span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span>$row<span class="token2">[</span><span class="token4">'id'</span><span class="token2">]</span><span class="token2">,</span><span class="token4">'typename'</span><span class="token">=</span><span class="token">></span>$row<span class="token2">[</span><span class="token4">'typename'</span><span class="token2">]</span><span class="token2">]</span><span class="token2">;</span> <span class="token2">}</span> $n<span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetTotalRow</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> 总记录数 新写法 global $db<span class="token2">;</span> $db<span class="token">-</span><span class="token">></span><span class="token1">getall</span><span class="token2">(</span><span class="token4">'arctype'</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 插入删除修改 ``` <pre class="calibre16">``` $sql <span class="token">=</span> <span class="token4">"insert into hx_arctype (typename) VALUES ('测试')"</span><span class="token2">;</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">ExecNoneQuery</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> $id <span class="token">=</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">GetLastID</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>插入id $sql <span class="token">=</span> <span class="token4">"delete from hx_arctype where id=12"</span><span class="token2">;</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">ExecNoneQuery</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> $sql <span class="token">=</span> <span class="token4">"update hx_arctype set typename='12' where id=13"</span><span class="token2">;</span> $dsql<span class="token">-</span><span class="token">></span><span class="token1">ExecNoneQuery</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> 新写法 global $db<span class="token2">;</span> $db<span class="token">-</span><span class="token">></span><span class="token1">insert</span><span class="token2">(</span><span class="token4">'demo'</span><span class="token2">,</span><span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> $id <span class="token">=</span> $db<span class="token">-</span><span class="token">></span><span class="token1">insertid</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>插入id $db<span class="token">-</span><span class="token">></span><span class="token1">update</span><span class="token2">(</span><span class="token4">'demo'</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'num +='</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">]</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $db<span class="token">-</span><span class="token">></span><span class="token1">delete</span><span class="token2">(</span><span class="token4">'demo'</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'id'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $db<span class="token">-</span><span class="token">></span><span class="token1">delete</span><span class="token2">(</span><span class="token4">'demo'</span><span class="token2">,</span> <span class="token4">"id IN ('"</span><span class="token2">.</span><span class="token1">implode</span><span class="token2">(</span><span class="token4">"','"</span><span class="token2">,</span> $deleteid<span class="token2">)</span><span class="token2">.</span><span class="token4">"')"</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//多选删除</span> ``` ```