-1

I have an array data called $req_dep. Here's what it look like when I var_dump() it.

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". How to use array in where condition like that?

Thank You

Zhorov
  • 28,486
  • 6
  • 27
  • 52
kitcat
  • 157
  • 1
  • 17

2 Answers2

1

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:

$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

Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
0

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();
Dum
  • 1,431
  • 2
  • 9
  • 23