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

后端 未结 3 894
悲哀的现实
悲哀的现实 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:45

    If you dig a bit into CodeIgniter's internals, you'll notice that the $this->db->like() function escapes special characters it contains - including, of course, %.

    I don't think like() will help you much with your particular needs. Your best bet, I guess, would be to bypass the problem and use a where function containing your LIKE clause:

    $this->db->select('*')->from('table')->where("column LIKE '%$keyword%'")->get()->result_array();
    

提交回复
热议问题