Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query bindbing for SQL LIKE in Codeigniter

Tags:

codeigniter

So, the following doesn't seem to work. What am I missing?

$sql = "SELECT item_id, item_name, category_name, user_data.value
                FROM
                    items, item_category, user_data
                WHERE
                    items.user_id = ?
                        AND item_name LIKE ?
                        AND item_location = user_data.id
                        AND item_category = category_id
                ORDER BY item_name";

$query = $this->db->query($sql, array($this->user_id, $search_term));
like image 655
sdot257 Avatar asked Jan 21 '23 02:01

sdot257


2 Answers

Ok, I figured it out. Everything else is fine, except I modified the binding as follows.

$query = $this->db->query($sql, array($this->user_id, '%'.$this->db->escape_like_str($search_term).'%'));
like image 158
sdot257 Avatar answered Jan 26 '23 01:01

sdot257


In CI4 you can use bindbing in this mode

$data = $this->getData();
$sql  = "SELECT * FROM users WHERE name_user LIKE :name_user: LIMIT 10";

$bind =
[
   'name_user' => "%{$data['name_user']}%",
];

return $this->db->query($sql,$bind)->getResultArray();
like image 29
the_martux Avatar answered Jan 25 '23 23:01

the_martux