## 更新数据表中的数据
~~~
Db::table('think_user')->where('id', 1)->update(['name' => 'thinkphp']);
~~~
如果数据中包含主键,可以直接使用:
~~~
Db::table('think_user')->update(['name' => 'thinkphp','id'=>1]);
~~~
> update 方法返回影响数据的条数,没修改任何数据返回 0
如果要更新的数据需要使用`SQL`函数或者其它字段,可以使用下面的方式:
~~~
Db::table('think_user')
->where('id', 1)
->update([
'login_time' => ['exp','now()'],
'login_times' => ['exp','login_times+1'],
]);
~~~
> `V5.0.18+`版本开始是数组中使用`exp`查询和更新的话,必须改成下面的方式:
~~~
Db::table('think_user')
->where('id', 1)
->update([
'login_time' => Db::raw('now()'),
'login_times' => Db::raw('login_times+1'),
]);
~~~
## 更新某个字段的值:
~~~
Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');
~~~
> setField 方法返回影响数据的条数,没修改任何数据字段返回 0
## 自增或自减一个字段的值
`setInc/setDec`如不加第二个参数,默认值为1
~~~
// score 字段加 1
Db::table('think_user')->where('id', 1)->setInc('score');
// score 字段加 5
Db::table('think_user')->where('id', 1)->setInc('score', 5);
// score 字段减 1
Db::table('think_user')->where('id', 1)->setDec('score');
// score 字段减 5
Db::table('think_user')->where('id', 1)->setDec('score', 5);
~~~
## 延迟更新
`setInc/setDec`支持延时更新,如果需要延时更新则传入第三个参数
下例中延时10秒,给`score`字段增加1
~~~
Db::table('think_user')->where('id', 1)->setInc('score', 1, 10);
~~~
> setInc/setDec 方法返回影响数据的条数
## 助手函数
~~~
// 更新数据表中的数据
db('user')->where('id',1)->update(['name' => 'thinkphp']);
// 更新某个字段的值
db('user')->where('id',1)->setField('name','thinkphp');
// 自增 score 字段
db('user')->where('id', 1)->setInc('score');
// 自减 score 字段
db('user')->where('id', 1)->setDec('score');
~~~
## 快捷更新(`V5.0.5+`)
`V5.0.5+`以上版本封装的`data`、`inc`、`dec`和`exp`方法属于链式操作方法,可以配合`update`使用(官方推荐用法)。
下面举个例子说明用法:
~~~
Db::table('data')
->where('id',1)
->inc('read')
->dec('score',3)
->exp('name','UPPER(name)')
->update();
~~~
- 简介
- PHP的优势
- PHP运行原理和机制
- PHP7有哪些新特性
- 第一章 PHP
- 基础
- 1、流程控制
- if...else
- switch
- for
- while
- foreach
- 跳出循环
- 2、数组函数
- 数组排序
- sort() - 以正序数组排序
- rsort() - 以降序对数组排序
- asort() - 根据值,以升序对关联数组进行排序
- ksort() - 根据键,以升序对关联数组进行排序
- arsort() - 根据值,以降序对关联数组进行排序
- krsort() - 根据键,以降序对关联数组进行排序
- 数组函数
- array_column() - 返回输入数组中某个单一列的值
- array_merge() - 把一个或多个数组合并为一个数组
- array_search() - 搜索数组中给定的值并返回键名
- array_shift() - 删除数组中首个元素,并返回被删除元素的值
- array_pop() - 删除数组的最后一个元素(出栈)
- array_product() - 计算数组中所有值的乘积
- array_push() - 将一个或多个元素插入数组的末尾(入栈)
- array_rand() - 返回数组中一个或多个随机的键
- array_sum() - 返回数组中值的和
- array_unique() - 删除数组中的重复值
- array_unshift() - 在数组开头插入一个或多个元素
- array_values() - 返回数组中所有的值
- in_array() - 检查数组中是否存在指定的值
- list() - 把数组中的值赋给一些变量
- 3、字符串函数
- strlen() - 返回字符串长度
- substr() - 截取字符串
- str_replace() - 把字符串的一部分替换为另一个字符串
- str_ireplace() - 替换字符串中的一些字符(对大小写不敏感)
- strstr() - 查找字符串在另一字符串中的第一次出现(对大小写敏感)
- stristr() - 查找字符串在另一字符串中第一次出现的位置(大小写不敏感)
- strripos() - 查找字符串在另一字符串中最后一次出现的位置(对大小写不敏感)
- strrpos() - 查找字符串在另一字符串中最后一次出现的位置(对大小写敏感)
- trim() - 移除字符串两侧的空白字符和其他字符
- rtrim() - 移除字符串右侧的空白字符或其他字符
- ltrim() - 移除字符串左侧的空白字符或其他字符
- strrev() - 反转字符串
- explode() - 把字符串打散为数组
- implode() - 数组转换为字符串
- strip_tags() - 剥去字符串中的 HTML 和 PHP 标签
- strtolower() - 把字符串转换为小写字母
- strtoupper() - 把字符串转换为大写字母
- htmlentities() - 把字符转换为 HTML 实体
- html_entity_decode() - 把 HTML 实体转换为字符
- md5() - 计算字符串的 MD5 散列
- md5_file() - 计算文件的 MD5 散列
- sha1() - 计算字符串的 SHA-1 散列
- sha1_file() - 计算文件的 SHA-1 散列
- 4、日期时间
- date() - 函数把时间戳格式化为更易读的日期和时间
- mktime() - 返回日期的 Unix 时间戳
- strtotime - 转化为时间戳
- 第二章 MYSQL
- 简介
- connect - 连接数据库
- create - 创建数据库和表
- insert - 添加数据
- select - 查询数据
- update - 修改数据
- delect - 删除数据
- where - 条件
- order by - 关键词
- 第三章 编辑器
- phpstorm
- 第四章 框架
- thinkphp5.0
- tp安装
- 开发规范
- 目录结构
- 数据库
- 查询数据
- 添加数据
- 更新数据
- 删除数据
- 查询方法
- 查询语法
- 链式操作
- where
- table
- alias
- field
- order
- limit
- page
- group
- having
- join
- union
- distinct
- lock
- cache
- comment
- fetchSql
- force
- bind
- partition
- strict
- failException
- sequence
- 聚合查询
- 时间查询
- 高级查询
- 视图查询
- 子查询
- 原生查询
- 事务操作
- 监听SQL
- 存储过程
- 分布式数据库
- 第五章 服务器
- 阿里云服务器
- linux命令
- nginx
- Nginx搭建负载均衡环境简单实现
- memcached
- redis
- 杂项
- http 状态码
- 消息
- 100 Continue
- 101 Switching Protocols
- 102 Processing
- 成功
- 200 OK
- 201 Created
- 202 Accepted
- 203 Non-Authoritative Information
- 204 No Content
- 205 Reset Content
- 206 Partial Content
- 207 Multi-Status
- 重定向
- 300 Multiple Choices
- 301 Moved Permanently
- 302 Move temporarily
- 303 See Other
- 304 Not Modified
- 305 Use Proxy
- 306 Switch Proxy
- 307 Temporary Redirect
- 请求错误
- 400 Bad Request
- 401 Unauthorized
- 402 Payment Required
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 407 Proxy Authentication Required
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 411 Length Required
- 412 Precondition Failed
- 413 Request Entity Too Large
- 414 Request-URI Too Long
- 415 Unsupported Media Type
- 416 Requested Range Not Satisfiable
- 417 Expectation Failed
- 421 too many connections
- 422 Unprocessable Entity
- 423 Locked
- 424 Failed Dependency
- 425 Unordered Collection
- 426 Upgrade Required
- 449 Retry With
- 451Unavailable For Legal Reasons
- 服务器错误
- 500 Internal Server Error
- 501 Not Implemented
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
- 505 HTTP Version Not Supported
- 506 Variant Also Negotiates
- 507 Insufficient Storage
- 509 Bandwidth Limit Exceeded
- 510 Not Extended
- 600 Unparseable Response Headers
- linux下自动执行任务
- restful api
- 富文本编辑器
- ueditor
- wangEditor
- js定时器
- 局域网访问本地项目
- 微信公众号网页授权登录
- 服务器配置https
- 配置虚拟目录
- ab压力测试
- 使用ab进行页面的压力测试
- apache性能测试工具ab使用详解
- 前后端分离之用户验证
- PHP面试题
- 题库一
- 题库二
- 题库三
- 题库四
- 题库五
- 题库六
- 算法题
- 星星算法
