ThinkPHP where()方法是 Model 類內置方法,用於設置數據庫查詢或者更新、刪除等操作條件。
where 方法支持以字符串、數組和對象方式來設置條件,該方法不能獨立使用,必須與 select()、find()、delete() 等數據操作方法搭配使用。
字符串方式條件即以字符串的方式將條件作爲 where() 方法的參數,例子:
$Dao = M("User"); $List = $Dao->where('uid<10 AND email="[email protected]"')->find();
使用數組方式的 where 條件例子:
$Dao = M("User"); // 構建查詢數組 $condition['uid'] = array('elt',10); $condition['email'] = "[email protected]"; $List = $Dao->where($condition)->find();
//查詢數據 if($uid){ //$wheresql = 'user_id = '.$uid; $wheresql['user_id'] = $uid; }else{ $wheresql=array(); } $data = Db::name('news')->where($wheresql)->select();