0

i am trying to get values from a comma seaparated field in my database column, my column looks like this:

enter image description here

i did the following code in my mode:

$this->db->select('*');
$this->db->where("pc", $mid);
$this->db->where_in("category", $lid);

$this->db->from('product');
$query = $this->db->get();
$result = $query->result();
return $result;

however this doesnt give the result correctly, can anyone please tell what is wrong in here, thanks in advance

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
TBC
  • 101
  • 7
  • 1
    The best solution here would be to refactor the database design to use a relational database properly [See this for why](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – RiggsFolly Jun 16 '23 at 13:28
  • Your code is not getting a single id from a comma delimited fields anywhere in this code. Do you mean you are trying to search for `2` in a column that contains `2,3` – RiggsFolly Jun 16 '23 at 13:31
  • @RiggsFolly yes, i am trying to search 2 in a column that contains 2,3 – TBC Jun 16 '23 at 13:32
  • 1
    As mentioned above, the root cause of this is your poor database design. Even if you solve this particular scenario, it will continue to cause you other problems. You should strongly consider normalising it properly. – ADyson Jun 16 '23 at 13:57
  • One of the linked answers suggests to use `FIND_IN_SET()` but you should know that this blocks the use of an index, so a query that searches that way will have to do a table-scan, and suffer poor performance. – Bill Karwin Jun 16 '23 at 16:41

0 Answers0