codeigniter数据库内连接

时间:2017-09-05 22:40:47

标签: php mysql sql codeigniter inner-join

我的错误是您没有关联这两个数据库,信息不正确,所有查询都会出现。我只想查看相应的查询users.id = conjuge.id

$this->db->where('users.id', $user_id);

  

错误消息:未定义的变量:user_id

型号:

class User_model extends CI_Model {

public function __construct() {
    parent::__construct();
    $this->load->database();
}

public function create_user($nome, $sobrenome, $username, $email, $password) {

    $data = array(
        'nome'  => $nome,
        'sobrenome' =>  $sobrenome,
        'username'   => $username,
        'email'      => $email,
        'password'   => $this->hash_password($password),
        'created_at' => date('j-m-Y H:i:s'),
    );
    return $this->db->insert('users', $data);

}

public function resolve_user_login($username, $password) {

    $this->db->select('password');
    $this->db->from('users');
    $this->db->where('username', $username);
    $hash = $this->db->get()->row('password');

    return $this->verify_password_hash($password, $hash);

}

public function get_user_id_from_username($username) {
    $this->db->select('id');
    $this->db->from('users');
    $this->db->where('username', $username);

    return $this->db->get()->row('id');

}

public function get_user($user_id) {

    $this->db->from('users');
    $this->db->where('id', $user_id);
    return $this->db->get()->row();

}

private function hash_password($password) {

    return password_hash($password, PASSWORD_BCRYPT);

}

private function verify_password_hash($password, $hash) {

    return password_verify($password, $hash);

}
public function get_conjuge(){
    $this->db->select('conjuge.id, conjuge.nome, conjuge.sobrenome, conjuge.cpf, conjuge.rg');
    $this->db->from('conjuge');
    $this->db->join('users', 'users.id = conjuge.id', 'inner');
    $this->db->where('users.id', $user_id);
    $query = $this->db->get();
    return $query->result();
}

我修改未解决

public function get_conjuge($user_id){
        $this->db->select('conjuge.id, conjuge.nome, conjuge.sobrenome, conjuge.cpf, conjuge.rg', 'users.id AS users');
        $this->db->from('conjuge');
        $this->db->join('users', 'users.id = conjuge.id', 'inner');
        $this->db->where('users.id', $user_id);
        $query = $this->db->get();
        return $query->result();
    }
  

遇到PHP错误

     

严重性:警告

     

消息:缺少User_model :: get_conjuge()的参数1,调用   /home/planoser/public_html/oauth/application/controllers/User.php on   第160行并定义

     

文件名:models / User_model.php

     

遇到PHP错误

     

严重性:注意

     

消息:未定义的变量:user_id

     

文件名:models / User_model.php

1 个答案:

答案 0 :(得分:0)

需要在查询用户中添加select,user_id,

$this->db->select('conjuge.id, conjuge.nome, conjuge.sobrenome, conjuge.cpf, conjuge.rg','users.user_id');

public function get_conjuge() {

}

然后将功能更改为带参数

public function get_conjuge($user_id) {

}