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('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.
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['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.
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.
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!