Home >PHP Framework >ThinkPHP >ThinkPHP where method: Set query or operation conditions
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().
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"')->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.
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['uid'] = array('elt',10); $condition['email'] = "Jack@163.com"; $List = $Dao->where($condition)->find();
This example has the same execution effect as the above example using string method.
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!