where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作。where方法的参数支持字符串和数组,虽然也可以使用对象但并不建议。
## 表达式查询
新版的表达式查询采用全新的方式,查询表达式的使用格式:
~~~
Db::table('think_user')
->where('id','>',1)
->where('name','thinkphp')
->select();
~~~
更多的表达式查询语法,可以参考[查询语法](https://www.kancloud.cn/manual/thinkphp5/135182)部分。
## 数组条件
可以通过数组方式批量设置查询条件。
### 普通查询
最简单的数组查询方式如下:
~~~
$map['name'] = 'thinkphp';
$map['status'] = 1;
// 把查询条件传入查询方法
Db::table('think_user')->where($map)->select();
// 助手函数
db('user')->where($map)->select();
~~~
最后生成的SQL语句是
~~~
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
~~~
### 表达式查询
可以在数组条件中使用查询表达式,例如:
~~~
$map['id'] = ['>',1];
$map['mail'] = ['like','%thinkphp@qq.com%'];
Db::table('think_user')->where($map)->select();
~~~
## 字符串条件
使用字符串条件直接查询和操作,例如:
~~~
Db::table('think_user')->where('type=1 AND status=1')->select();
~~~
最后生成的SQL语句是
~~~
SELECT * FROM think_user WHERE type=1 AND status=1
~~~
使用字符串条件的时候,建议配合预处理机制,确保更加安全,例如:
~~~
Db::table('think_user')->where("id=:id and username=:name")->bind(['id'=>[1,\PDO::PARAM_INT],'name'=>'thinkphp'])->select();
~~~
- 简介
- 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面试题
- 题库一
- 题库二
- 题库三
- 题库四
- 题库五
- 题库六
- 算法题
- 星星算法
