ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# thinkphp常用判断语句 运算符: **eq\*\*\*\*相等** **ne、neq\*\*\*\*不相等** **gt\*\*\*\*大于** **lt\*\*\*\*小于** \*\*egt\*\*\*\*大于等于 \*\* **elt\*\*\*\*小于等于** **not\*\*\*\*非** **mod\*\*\*\*求模** 数据库的加减: thinkphp中setInc、setDec方法 可用于统计字段(通常是数字类型的字段)的更新,例如积分,等级,登陆次数等 必须配合连贯操作where一起使用 ``` <pre class="calibre13">``` <span class="token5">1</span> $User <span class="token">=</span> <span class="token5">M</span><span class="token1">(</span><span class="token4">"User"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 实例化User对象</span> <span class="token5">2</span> $User<span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'id=5'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">setInc</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">,</span><span class="token5">3</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 用户的积分加3</span> <span class="token5">3</span> $User<span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'id=5'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">setInc</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 用户的积分加1</span> <span class="token5">4</span> $User<span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'id=5'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">setDec</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">,</span><span class="token5">5</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 用户的积分减5</span> <span class="token5">5</span> $User<span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'id=5'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">setDec</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 用户的积分减1</span> ``` ``` 添加数据后如果需要返回新增数据的自增主键,可以使用`getLastInsID`方法: ``` <pre class="calibre13">``` Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'user'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">insert</span><span class="token1">(</span>$data<span class="token1">)</span><span class="token1">;</span> $userId <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'user'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">getLastInsID</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> ``` ``` 或者直接使用`insertGetId`方法新增数据并返回主键值: ``` <pre class="calibre14">``` Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'user'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">insertGetId</span><span class="token1">(</span>$data<span class="token1">)</span><span class="token1">;</span> ``` ```