Home > PHP Framework > ThinkPHP > Introduction to ThinkPHP where method

Introduction to ThinkPHP where method

Release: 2020-04-02 10:13:25
forward
3520 people have browsed it

Introduction to ThinkPHP where method

ThinkPHP where()

ThinkPHP where() method is a built-in method of the Model class, used to set up database queries or update, delete and other operations condition.

The where method supports setting conditions in string, array, and object modes. This method cannot be used independently and must be used in conjunction with data operation methods such as select(), find(), and delete().

String mode

The string mode condition is to use the condition as a string as a parameter of the where() method, example:

$Dao = M("User");
$List = $Dao->where(&#39;uid<10 AND email="Jack@163.com"&#39;)->find();
Copy after login

The actual executed SQL is:

SELECT * FROM user WHERE uid<10 AND email="Jack@163.com" LIMIT 1
Copy after login

The conditions set in string mode are the conditions for actual SQL execution, and are the closest to native SQL. ThinkPHP will not do any (type) checks on the conditions.

Array method

In most cases it is recommended to use index arrays or objects as query conditions, because it will be safer. For details, see: "ThinkPHP Type Detection" .

Example of where condition using array method:

$Dao = M("User");
// 构建查询数组
$condition[&#39;uid&#39;] = array(&#39;elt&#39;,10);
$condition[&#39;email&#39;] = "Jack@163.com";
$List = $Dao->where($condition)->find();
Copy after login

This example has the same execution effect as the above example using string method.

Using objects

where method can also use objects to set query or operation conditions, and any object can be used. Take the stdClass built-in object as an example:

$Dao = M("User");
// 定义查询条件
$condition = new stdClass(); 
$condition->uid = array(&#39;elt&#39;,10);
$condition->email = "Jack@163.com";
$List = $Dao->where($condition)->find();
Copy after login

The conditional effects of using object mode and array mode are the same and are interchangeable.

Recommended tutorial: thinkphp tutorial

The above is the detailed content of Introduction to ThinkPHP where method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:oschina.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template