不确定ci的用户查询是否正确

时间:2014-06-23 09:26:55

标签: php codeigniter

我正在尝试将旧用户查询select*from转换为新的codeigniter方式,但我不确定我是否正确。

这是我以前的方式

$user_query = $this->db->query(
        "SELECT * FROM " . DB_PREFIX . "user WHERE username = '" .
        $this->db->escape($username) . 
        "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" .
        $this->db->escape($password) . "'))))) OR password = '" . 
        $this->db->escape(md5($password)) . "') AND status = '1'"
    );

这是新方式。我不确定select * from是否正确我已经删除了凌乱的盐和SHA1

public function login() {
        $this->db->select('user');

        $this->db->from('user_id');

        $this->db->where('username', $this->input->post('username'));

        $password = $this->input->post('password');

        // Password Has Own Security Measures
        $this->db->where('password',  $this->encryption->hash_password().hash('sha512', $password).$this->encryption->generate_salt());

        $this->db->where('status', "1");

        $user_query = $this->db->get('user');

        if($user_query->num_rows) {

        } else {
            /*
            | Other Data Goes Here.
            |
            |
             **/
            return true;
        }
      return false;
  }

2 个答案:

答案 0 :(得分:0)

你混淆了active record函数,你可以链接它,比如:

....
$password = $this->input->post('password');
$username = $this->input->post('username');

$user_query = $this->db->select('user_id')    
->where( 
    array(
        'username' => $username,
        'password' => $this->encryption->hash_password().hash('sha512', $password).$this->encryption->generate_salt(),
        'status'   => 1
    )
)
->get('user');
.....

答案 1 :(得分:0)

您可以在CodeIgniter中链接数据库,并且您在from:

中遇到了一些错误
$password = $this->input->post('password');

$query = $this->db->select('*')
            ->where('username', $this->input->post('username'))
            ->where('password',  $this->encryption->hash_password().hash('sha512', $password).$this->encryption->generate_salt())
            ->where('status', "1")
            ->get('user');