发生数据库错误错误号:1054 'where clause' 中的未知列 'Array'

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

我无法从数组中获取值 $list_izin,如何从数组中获取值并为 where 子句条件设置值

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

您看到 "unknown column 'Array'" 错误是因为当 where() 处理关联数组的值时,ID 数组被转换为字符串。 ("Array"是PHP中任意数组的字符串表示。)

要修复它,请先从 $where 数组中删除最后一列。

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

然后在您的 select 查询中,在 where_in() 条件下添加 ID 列表。

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

where_in() 将添加到 where() 中定义的现有条件,不会替换它。