Codeigniter $ this-> db-> where (); custom string problem

I am trying to select some values โ€‹โ€‹using a custom string. below is my code

$this->db->from('posted'); $st="infor='rent' AND (typeq='in' OR typeq='out')"; $this->db->where($st); $q = $this->db->get(); 

Database error occurred

 Error Number: 1054 Unknown column 'infor='rent'' in 'where clause' SELECT * FROM (`posted_ads`) WHERE `infor='rent'` AND (typeq='in' OR typeq='out') Filename: C:\wamp\www\parklot\system\database\DB_driver.php Line Number: 330 

I think the problem is coz

 WHERE `infor='rent'` 

when I execute manualy this code, it works fine.

 WHERE infor='rent' 

how to get rid of

 `` 

as it automatically adds

+7
source share
2 answers

Add the third parameter to where() and set it to FALSE

  $this->db->from('posted'); $st="infor='rent' AND (typeq='in' OR typeq='out')"; $this->db->where($st, NULL, FALSE); $q = $this->db->get(); 

$this->db->where() takes an optional third parameter. If you set the value to FALSE , CodeIgniter will not attempt to protect field or table names with check marks.

CodeIgniter Documentation

+22
source

While the solution works, I want to add: be careful! You must protect your request and avoid all values! If you like to use Query Builder

 $q = $this->db->select('*')->from('posted_ads') ->where('infor', 'rent') ->or_group_start() ->where('typeq', 'in') ->where('typeq', 'out') ->group_end() ->get(); 

In this way, Codeigniter takes care of the correct escape.

0
source

All Articles