简体   繁体   English

在 where 条件下使用数组

[英]using array in where condition

I have an array data called $req_dep .我有一个名为$req_dep的数组数据。 Here's what it look like when I var_dump() it.这是我使用var_dump()样子。

array(2) { [0]=> int(41) [1]=> int(765) }

I want to use that data in query我想在查询中使用该数据

  select * from dbo.RequisitionTable 
  where RequestorID = '$ID'
  and RequestorDepartmentID in ($req_dep)
  and IsProcessedToHire=0 
  and IsHold = 0
  and IsRejected = 0

But it always get error "Array to string conversion" .但它总是得到错误"Array to string conversion" How to use array in where condition like that?如何在这样的条件下使用数组?

Thank You谢谢你

Your query:您的查询:

select * from dbo.RequisitionTable 
where RequestorID = '$ID'
and RequestorDepartmentID in ($req_dep)
and IsProcessedToHire=0 
and IsHold = 0
and IsRejected = 0

Can easily be converted to CodeIgniter's query builder like this:可以很容易地转换为 CodeIgniter 的查询构建器,如下所示:

$this->db->select('*');
$this->db->from('dbo.RequisitionTable');
$this->db->where('RequestorID', $ID);
$this->db->where_in('RequestorDepartmentID', $req_dep);
$this->db->where('IsProcessedToHire', 0);
$this->db->where('IsHold', 0);
$this->db->where('IsRejected', 0);

The above assumes $ID and $req_dep are passed to the model and that the latter is an array以上假设 $ID 和 $req_dep 被传递给模型,后者是一个数组

Why don't you use Query builder class ?为什么不使用 查询构建器类 You can do that by,你可以这样做,

$conds = [
   "RequestorID" => $ID,
   "IsProcessedToHire" => 0,
   "IsHold" => 0,
   "IsRejected" => 0  
];

$result = $this->db->where($conds)->where_in("RequestorDepartmentID", $req_dep)->get("RequisitionTable")->result_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM