codeigniter

$this->db->escape() function adding single quote in codeigniter


In codeigniter when i am using function $this->db->escape() while insert data, It is adding single quote in database, Can anyone please help why i am getting this issue ?

Here is my code

$data = array('company_id'=>$this->db->escape($companyID),'payor_type'=>$this->db->escape($payor_type),
                'payer_type'=>$this->db->escape($payer_type),'insurance'=>$this->db->escape($insurance),
                'created_date'=>date("Y-m-d H:i:s"));
                $this->db->insert('tb_Payer',$data);

Solution

  • When you use the query builder class to construct your queries the values are escaped automatically by the system so you don't have to use the function $this->db->escape. In your case, each value was escaped by the escape function and the system did it again for each value when executing the insert function.

    Now if you want to run custom queries using the function $this-db->query it is a good practice to escape the data like bellow:

    $sql = "INSERT INTO table (column) VALUES(".$this->db->escape($value).")";
    $this->db->query($sql);