Home  >  Article  >  PHP Framework  >  ThinkPHP where method: Set query or operation conditions

ThinkPHP where method: Set query or operation conditions

步履不停
步履不停Original
2019-06-25 17:14:096207browse

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();

The actual executed SQL is:

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

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();

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();

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!

Statement:
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