在Codeigniter中将查询更改为活动记录

时间:2017-11-22 17:21:10

标签: php codeigniter activerecord

我想将此代码更改为活动记录查询,我很难这样做。

 public function get_user($username, $password) {
    $p = md5($password);
    $q = "SELECT u.userid, u.userfullname, u.useraddress
    FROM user u 
    WHERE u.username = ? AND u.userpassword = ?";

    $r = $this->db->query($q, [$username, $p]);
    $this->db->close();
    return $r->result_object();
}

2 个答案:

答案 0 :(得分:0)

当您处理单个表时,您不需要表别名“u”。如果您指的是CodeIgniter现在称之为查询生成器的内容,那么这就是您的答案:

$r = $this->db->select('userid, userfullname, useraddress')
    ->where('username', $username)
    ->where('userpassword', $p)
    ->get('user');

if( $r->num_rows() > 0 )
    return $r->result();

return FALSE;

答案 1 :(得分:0)

https://www.codeigniter.com/userguide3/database/query_builder.html

return $this->db->select('userid, userfullname, useraddress')
->where('username', $username)
->where('userpassword', $p)
->get('user')->row();

// if no entry null will be returned
//row returns user as an object row_array() returns user as an array

也可以代替md5查看他们的加密库 https://www.codeigniter.com/user_guide/libraries/encryption.html