Search code examples
phpcodeignitercodeigniter-2codeigniter-url

call the ' id ' of the mysql database with varchar format but Unknown column 'GE' in 'where clause'


I tried to call up data by 'id' format id example id = GE-DIS-001 to JOIN table with Codeigniter , the result of "Unknown column ' GE ' in 'where clause ' what's wrong ??

Controller :

    public function detail(){

    $id = $this->uri->segment(4);
    $detail = $this->mcrud->get_detail($id); 

    $data = array(
                'detail'=>$detail,
                'lihat' =>$this->mcrud->lihat_komentar($id),
                'isi'       =>'instrument/read_views');
    $this->load->view('layout/wrapper', $data);


   }    

Model :

    public function get_detail($id){

    $sql = "SELECT * FROM tbdetail 
            JOIN tbkalibrasi ON tbkalibrasi.id = tbdetail.id
            JOIN tbsupplier ON tbsupplier.namasupplier = tbdetail.namasupplier

            WHERE tbdetail.id = {$id}
            ";
    return $this->db->query($sql)->result_array();
    }   

Solution

  • It seems $id variable contains string with spaces, so you are getting this error. Change it to

    public function get_detail($id){
    
        $sql = "SELECT * FROM tbdetail 
                JOIN tbkalibrasi ON tbkalibrasi.id = tbdetail.id
                JOIN tbsupplier ON tbsupplier.namasupplier = tbdetail.namasupplier
    
                WHERE tbdetail.id = '$id'
                ";
        return $this->db->query($sql)->result_array();
        }