为什么查询不是由codeigniter db运行的?

时间:2015-05-27 09:41:36

标签: php mysql codeigniter

我在codeigniter中运行查询时遇到问题。

这是代码:

class Login_Model extends CI_Model {

    public function user_login($username, $password) {
        $sql = "select username,password from login_users where username='$username' && password='$password'";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }

}

我不知道什么是错误但查询没有运行并返回错误。我正在使用codeigniter 3.请指导我

4 个答案:

答案 0 :(得分:1)

使用' AND'而不是'&&'

select username,password from login_users where username='$username' AND password='$password'

答案 1 :(得分:1)

试试这个

class Login_Model extends CI_Model {

    public function user_login($username, $password) {
        $query = $this->db->query("SELECT username,password FROM login_users WHERE username='$username' AND password='$password'");
        $result = $query->result_array();
        return $result;
    }
}

答案 2 :(得分:1)

使用密码和用户名变量,例如$username = $this->input->post('username');$password = $this->input->post('password');

function user_login() {
$username = $this->input->post('username');
$password = $this->input->post('password');

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

$query = $this->db->get('login_users');

if ($query->num_rows() > 0) {
    // Or return $query->result_array();
    return $query->row_array();
} else {
    return false;
}
}

我还建议使用php password_hash(); 来创建密码,并使用 password_verfiy()来检查密码

http://php.net/manual/en/function.password-verify.php

http://php.net/manual/en/function.password-hash.php

仅使用回调示例登录

public function index() {

$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_checkuser');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run() == FALSE) {
    $this->load->view('login_view');
} else {
   redirect('user_cp');
}

}

public function checkuser() {
$this->load->library('form_validation');
$this->load->model('model_user');

if ($this->model_user->user_login() == FALSE) {

$this->form_validation->set_message('checkuser', 'User Login Information Incorrect');

return FALSE;

} else {

return TRUE;

}

}

答案 3 :(得分:1)

使用Active Record查询

$this->db->select('username,password');
$this->db->from('login_users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get();

if ($query->num_rows() > 0) {
    $row = $query->result_array();
    return $row;
}