Codeigniter: Weird behaviour of $this->db->like()

后端 未结 3 876
悲哀的现实
悲哀的现实 2020-12-16 19:19

I\'ve written a simple query for searching a keyword in the database.

$keyword = \"keyword sample\"; 
$keyword = str_replace(\" \", \"%\", $keyword);   

$th         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-16 19:27

    Use the escape_like_str() method.

    The escape_like_str() method should be used when strings are to be used in LIKE conditions so that LIKE wildcards %, _ in the string are also properly escaped. It cannot automatically add the ESCAPE ! condition for you, and so you’ll have to manually do that.

    Hope it helps.

    $keyword = "keyword sample"; 
    $sql = "SELECT id FROM table WHERE column LIKE '%" .
    $this->db->escape_like_str($keyword)."%' ESCAPE '!'";
    

    Source:- https://www.codeigniter.com/userguide3/database/queries.html

提交回复
热议问题