Search code examples
phpcodeigniteractiverecord

codeigniter conditions where when field name contains space


i have problem using active record, if i have active record like this

$data_array = array(
  'Code Employee' => 'AC01',
  'Status Employee' => 'Y'
);

$this->db->where($data_array);
$this->db->get('masteremp');
echo $this->db->last_query();

//Output query will be
SELECT * FROM masteremp WHERE `Code` `Employee` = 'AC01' AND `Status` `Employee` = 'Y'

i try edit DB_query_builder and i add this on line 2430

if(!empty($conditions)){
  for($index = 0; $index < sizeof($conditions); $index++){
    if (strpos($conditions[$index], '` `') !== false) {
      $conditions[$index] = str_replace('` `', ' ', $conditions[$index]);
    }
  }
}

do u have method for field with space? i think my method is not efficiency.. thanks..


Solution

  • IN codeigniter you can escape values and identifiers.Try once like this..

    Replace

    $this->db->where($data_array);
    

    With

    $this->db->where($data_array,TRUE);//second parameter as TRUE
    

    Then

    //Output query will be
    SELECT * FROM masteremp WHERE `Code Employee` = 'AC01' AND `Status Employee` = 'Y'