0

In one of my CodeIgniter based application, I store the country data in a column as comma separated value [It is possible to pass multiple country from front-end and rather using another table as mapping (using foreign_key) I use one single table].

To fetch the result in a search page, I wrote the following query:

$this->db->like('country', $country_id);

What the problem I face is, suppose row_1 contains the following data for my target column: 1,3,17 and now if I pass $country_id = 7, it fetches row_1, although row_1 does not directly have 7 (but have 17)...

I know that the problem occurs as CodeIgniter converts my query as %7%.

So my question: Is there any way to omit % character from before and after the search_creteria in CodeIgniter?

I mean, CodeIgniter will convert my query as:

SELECT * FROM table_name WHERE country LIKE '7'

rather (what is currently do):

SELECT * FROM table_name WHERE country LIKE '%7%'
  • Thanks
Shimul
  • 463
  • 2
  • 7
  • 33
  • 2
    The answer is to restructure your table where you don't have a comma separated list of values in a single cell (You should never really need this anyways, there is almost always a better way to do that), or fetch just like you are right now and loop through the results to check if it's a true match – GrumpyCrouton Jan 09 '19 at 17:46
  • Try `$this->db->like('country', $country_id, 'none');` to remove the wildcards although it won't do what you intend it to do, GrumpyCrouton is correct. – Kisaragi Jan 09 '19 at 17:52

0 Answers0