Home > PHP Framework > ThinkPHP > body text

ThinkPHP where method: Set query or operation conditions

步履不停
Release: 2019-06-25 19:42:39
Original
6230 people have browsed it

ThinkPHP where method: Set query or operation conditions

ThinkPHP where()

ThinkPHP where() method is a built-in method of the Model class, used to set operating conditions such as database query or update, delete, etc.

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('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('elt',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.

ThinkPHP where When using array or object methods, ThinkPHP-specific query expressions must be used. For details, see "ThinkPHP Expressions".

For more ThinkPHP related technical articles, please visit the ThinkPHP Tutorial column to learn!

The above is the detailed content of ThinkPHP where method: Set query or operation conditions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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