开发者

Query bindbing for SQL LIKE in 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));


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).'%'));


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜