【问题标题】:A Database Error Occurred Error Number: 1054 Unknown column 'Array' in 'where clause'发生数据库错误错误号:1054 'where 子句'中的未知列 'Array'
【发布时间】:2019-01-14 21:05:31
【问题描述】:

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

【问题讨论】:

  • 使用where_in()or_where_in()
  • 抱歉,刚刚发现复制只是部分适用

标签: php codeigniter codeigniter-3 codeigniter-2


【解决方案1】:

您会看到“未知列'数组'”错误,因为当where() 处理关联数组的值时,ID 数组正在转换为字符串。 (“Array”是 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() 中定义的现有条件中,它不会替换它。

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2016-03-19
    • 2023-02-14
    • 2019-08-16
    相关资源
    最近更新 更多