发生数据库错误,错误号:1054“ where子句”中的未知列“ Array”

时间:2019-01-14 21:05:31

标签: php codeigniter codeigniter-3 codeigniter-2

我无法从数组中获取值$ 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();
}

1 个答案:

答案 0 :(得分:2)

您看到“未知列'Array'”错误,因为当where()处理关联数组的值时,ID数组正在转换为字符串。 (“数组”是PHP中任何数组的字符串表示形式。)

要解决此问题,请首先从$where数组中删除最后一列。

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

然后在您的选择查询中以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()中定义的现有条件中,不会替代它。

相关问题