使用数组作为查询条件 ,字段之间的默认逻辑关系是 逻辑与 AND, $condition['_logic'] = 'OR';

二、使用数组作为查询条件
这种方式是最常用的查询方式,例如:
ThinkPHP3.2.3快速入门
本文档使用 看云 构建 - 21 -
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['status'] = 1;
// 把查询条件传入查询方法
$User->where($condition)->select();
最后生成的SQL语句是
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
如果进行多字段查询,那么字段之间的默认逻辑关系是 逻辑与 AND,但是用下面的规则可以更改默认的
逻辑判断,通过使用 _logic 定义查询逻辑:
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// 把查询条件传入查询方法
$User->where($condition)->select();
最后生成的SQL语句是
SELECT * FROM think_user WHERE `name`='thinkphp' OR `account`='thinkphp'

 

    //http://localhost/thinkphp323/index.php/home/User/select_score?score=100
    public function select_score($score=1){
        $User = M("User"); // 实例化User对象
        $u=$User->where('score=' . $score)->select();
        var_dump($u);
    }


    //http://localhost/thinkphp323/index.php/home/User/select_user_score?score=100&username=jim
    public function select_user_score($username='jim',$score=100){
        $User = M("User"); // 实例化User对象
        $condition['username'] = $username;
        $condition['score'] = $score;
        // 把查询条件传入查询方法
        $u=$User->where($condition)->select();
        var_dump($u);
    }
array (size=1)
  0 => 
    array (size=4)
      'id' => 

string

 '1' (length=1)
      'username' => 

string

 'jim' (length=3)
      'score' => 

string

 '100' (length=3)
      'create_time' => 

string

 '0' (length=1)

posted @ 2017-11-24 17:11  sky20080101  阅读(91)  评论(0)    收藏  举报