多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 第四课数据库操作 ### 常用表说明 ``` <pre class="calibre14">``` ims_mc_mapping_fans 粉丝表 ims_mc_members 会员表 ``` ``` ### 数据库操作函数 ``` <pre class="calibre16">``` 表名 <span class="token1">tablename</span><span class="token2">(</span><span class="token4">'mc_members'</span><span class="token2">)</span> 查询一条数据 <span class="token1">pdo_get</span><span class="token2">(</span><span class="token4">'yoby_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> <span class="token1">pdo_get</span><span class="token2">(</span><span class="token4">'yoby_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><span class="token4">'title'</span><span class="token2">,</span><span class="token4">'num'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span>返回特定字段 <span class="token1">pdo_get</span><span class="token2">(</span><span class="token4">'yoby_demo'</span><span class="token2">,</span><span class="token2">[</span><span class="token2">]</span><span class="token2">,</span><span class="token2">[</span><span class="token4">'count(*) as z'</span><span class="token2">,</span><span class="token4">'title'</span><span class="token2">,</span><span class="token4">'num'</span><span class="token2">,</span><span class="token4">'max(num)'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">pdo_fetch</span><span class="token2">(</span><span class="token4">"SELECT username, uid FROM "</span><span class="token2">.</span><span class="token1">tablename</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token2">.</span><span class="token4">" WHERE uid = :uid LIMIT 1"</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">':uid'</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="token1">pdo_getcolumn</span><span class="token2">(</span><span class="token4">'yoby_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="token4">'title'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">pdo_fetchcolumn</span><span class="token2">(</span><span class="token4">"SELECT COUNT(*) FROM "</span><span class="token2">.</span><span class="token1">tablename</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> 查询多条记录 表名<span class="token2">,</span>条件<span class="token2">,</span>返回字段<span class="token2">,</span>主键<span class="token2">,</span>排序<span class="token2">,</span>限制条数 <span class="token1">pdo_getall</span><span class="token2">(</span><span class="token4">'yoby_demo'</span><span class="token2">,</span><span class="token2">[</span><span class="token2">]</span><span class="token2">,</span><span class="token2">[</span><span class="token2">]</span><span class="token2">,</span><span class="token4">''</span><span class="token2">,</span><span class="token4">'id desc'</span><span class="token2">,</span><span class="token4">'.($pindex-1)* $psize.'</span><span class="token2">,</span>'<span class="token2">.</span>$psize<span class="token2">)</span><span class="token2">;</span> <span class="token1">pdo_fetchall</span><span class="token2">(</span><span class="token4">"SELECT username, uid FROM "</span><span class="token2">.</span><span class="token1">tablename</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token2">,</span> <span class="token2">[</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> 插入数据<span class="token2">,</span>第二个参数数组 <span class="token1">pdo_insert</span><span class="token2">(</span><span class="token4">'yoby_demo'</span><span class="token2">,</span><span class="token2">[</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $id <span class="token">=</span> <span class="token1">pdo_insertid</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>插入id 修改 <span class="token1">pdo_update</span><span class="token2">(</span><span class="token4">'yoby_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> 删除 <span class="token1">pdo_delete</span><span class="token2">(</span><span class="token4">'yoby_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> <span class="token1">pdo_delete</span><span class="token2">(</span>$this<span class="token">-</span><span class="token">></span>table_reply<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> 执行sql <span class="token1">pdo_query</span><span class="token2">(</span><span class="token4">"DELETE FROM "</span><span class="token2">.</span><span class="token1">tablename</span><span class="token2">(</span><span class="token4">'users'</span><span class="token2">)</span><span class="token2">.</span><span class="token4">" WHERE uid = :uid"</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">':uid'</span> <span class="token">=</span><span class="token">></span> <span class="token3">2</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> 支持多条sql用分号隔开 <span class="token1">pdo_run</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> 显示调试语句 <span class="token1">pdo_debug</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> 检测某个字段是否存在 <span class="token1">pdo_fieldexists</span><span class="token2">(</span><span class="token4">'shopping_goods'</span><span class="token2">,</span> <span class="token4">'credit'</span><span class="token2">)</span><span class="token2">;</span> 检测某个表是否存在 <span class="token1">pdo_tableexists</span><span class="token2">(</span>$tablename<span class="token2">)</span> 检测表是否为空 <span class="token1">pdo_exists</span><span class="token2">(</span>$tb<span class="token2">)</span> 获取数据条数 <span class="token1">pdo_count</span><span class="token2">(</span>$tb<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> ``` ```