Search code examples
phpcodeignitercodeigniter-3codeigniter-2

A Database Error Occurred Error Number: 1054 Unknown column 'Array' in 'where clause'


i cannot get value $list_izin from array, how can i get value from array and set the value for where clause condition

$list_izin = ['7','11','14','16','19','202','139','157'];

$where = array(
    'tmuser_userauth.userauth_id' => $userauth_id,
    'tmpegawai.bidangid' => $bidangid,
    'trperizinan_user.trperizinan_id' => $list_izin
    );
    }
    return $this->db
            ->select('
                tmpegawai.pegawaiid,
                tmpegawai.n_pegawai,
                tmpegawai.telp,
                tmuser.photo,
                tmuser.last_login,
                trperizinan_user.trperizinan_id

            ')
            ->from($this->table)
            ->join('tmuser', 'tmuser.tmpegawai_id = tmpegawai.pegawaiid')
            ->join('tmuser_userauth','tmuser_userauth.tmuser_id = tmuser.id')
            ->join('trperizinan_user','trperizinan_user.user_id = tmuser.id')
            ->where($where)
            ->get();
}

Solution

  • You're seeing the "unknown column 'Array'" error because the array of IDs is being converted to a string when that value of the associative array is processed by where(). ("Array" is the string representation of any array in PHP.)

    To fix it, first remove that last column from the $where array.

    $where = array(
        'tmuser_userauth.userauth_id' => $userauth_id,
        'tmpegawai.bidangid' => $bidangid,
    );
    

    Then in your select query add the list of IDs in a where_in() condition.

    return $this->db
        ->select('
            tmpegawai.pegawaiid,
            tmpegawai.n_pegawai,
            tmpegawai.telp,
            tmuser.photo,
            tmuser.last_login,
            trperizinan_user.trperizinan_id
    
        ')
        ->from($this->table)
        ->join('tmuser', 'tmuser.tmpegawai_id = tmpegawai.pegawaiid')
        ->join('tmuser_userauth','tmuser_userauth.tmuser_id = tmuser.id')
        ->join('trperizinan_user','trperizinan_user.user_id = tmuser.id')
        ->where($where)
        ->where_in('trperizinan_user.trperizinan_id', $list_izin)
        ->get();
    

    The where_in() will be added to the existing criteria defined in the where(), it won't replace it.