phpmysqlcodeigniterjoinambiguous

"Column name in WHERE clause is ambiguous" error from CodeIgniter query with a joined table


I want to fetch data from two tables but I got this error:

enter image description here

Here is my Query from Model:

public function tampil_edit($id)
{
    $this->db->join('tb_m_user', 'tb_m_user.id=tb_m_notaris.id');
    $this->db->select('tb_m_notaris.*,tb_m_user.email as email_notaris');
    return $this->db->get_where('tb_m_notaris', $id);
}

Here is my Controller :

public function tampiledit($id)
{
    $id = ['id' => $id];
    $title['title'] = 'Notaris | Edit';
    $data['notaris'] = $this->m_notaris->tampil_edit($id)->result();

    $this->load->view('template/headercss', $title);
    $this->load->view('template/sidebar');
    $this->load->view('template/navbar');
    $this->load->view('master_data/notaris/edit', $data);
    $this->load->view('template/footerjs');
}

Here Is what the $id contain:

enter image description here


Solution

  • Check your SQL

    WHERE id = 45
          ^^
    

    This id belongs to which table? I notice there are multiple table (tb_m_notaris,tb_m_user) with id column.

    To call function

    tampil_edit(45) {} # makesure $id is not an array
    

    In model

    public function tampil_edit($id) {
        $this->db->select('tb_m_notaris.*,tb_m_user.email as email_notaris');
        $this->db->from('tb_m_notaris');
        $this->db->join('tb_m_user', 'tb_m_user.id = tb_m_notaris.id');
        $this->db->where('tb_m_notaris.id', $id);
        return $this->db->get()->result();
    }