php - Codeigniter Query Builder - Need to check if entry exists in multiple columns
We have a table with multiple attributes, such as:
TABLE_ATTRIBUTES
ID, Color, Size, Weight
111, Red, XL null
112, null, SM 10
I want to build a method inside my model, that returns the row ID, by checking all the columns for any attributes. For example, if I want to pass 'green', 'SM', '9'.
What's the best way to build this Codeigniter DB Query?
I have something like this, but it's not working:
$this->db->select('id');
->or_where($color === NULL ? 'color IS NOT NULL' : 'color =', $color)
->or_where($size === NULL ? 'size IS NOT NULL' : 'size =', $size)
->or_where($weight === NULL ? 'weight IS NOT NULL' : 'weight =', $weight_10)
$row = $this->db->get('product')->row();
Answer
Solution:
Your first
or_where
should just bewhere
according to the documentation so you probably want to alter it slightly:See this doc about
$this->db->or_where()
for their example: https://www.codeigniter.com/userguide3/database/query_builder.html