Search code examples
phpmysqlsqlcodeigniterinner-join

codeigniter Database inner Join


My error is that you are not associating the two databases, the information is not correct, all queries appear. I want to see only the corresponding query, users.id = conjuge.id

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

Error Message: Undefined variable: user_id

Model:

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();
}

I modify not solved

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();
    }

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for User_model::get_conjuge(), called in /home/planoser/public_html/oauth/application/controllers/User.php on line 160 and defined

Filename: models/User_model.php

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: user_id

Filename: models/User_model.php


Solution

  • Need to add select in query users,user_id,

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

    Then Change function to with parameters

    public function get_conjuge($user_id) {
    
    }