三、使用对象方式来查询

三、使用对象方式来查询
这里以stdClass内置对象为例:
$User = M("User"); // 实例化User对象
// 定义查询条件
$condition = new stdClass();
$condition->name = 'thinkphp';
$condition->status= 1;
$User->where($condition)->select();
最后生成的SQL语句和上面一样
SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
使用对象方式查询和使用数组查询的效果是相同的,并且是可以互换的,大多数情况下,我们建议采用数
组方式更加高效。

 

    //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;
        $condition['_logic'] = 'OR';
        */

        $User = M("User"); // 实例化User对象
        // 定义查询条件
        $condition = new \stdClass();
        $condition->username = $username;
        $condition->score = $score;
        $condition->_logic = 'OR';
        $User->where($condition)->select();

        // 把查询条件传入查询方法
        $u=$User->where($condition)->select();
        var_dump($u);
    }

 

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