简体   繁体   中英

Can't use CodeIgniter's LIKE()

Because of some bug or my lack of knowledge in ODBC connections I can't use CodeIgniter's

$this->db->like();

(If you really want to know why I can't use it, see my other thread here .)

How do I replace

$this->db->like("name", 'a', 'after');

with some other safe code?

EDIT:

Obviously, I wasn't clear in my description.

I KNOW how to use "like()". My problem is that I CAN'T use it because of other circumstances. What I need is a substitute for "like()".

I know I could do it like:

$this->db->where("name LIKE 'a%'", NULL, FALSE);

but that wouldn't be safe.

EDIT 2:

Maybe this could work:

$user_input = "a";

//Escape input
$escaped_input = $this->db->escape($user_input);

//add a %-sign to the end of the escaped input
$like_input = substr_replace($escaped_input, "%", -1, 0)

$this->db->where("name LIKE " . $like_input, NULL, FALSE);

But I get the feeling it would not prevent SQL injections.

There is 3 methods to follow.

  1. after

     $this->db->like('name', 'a', 'after'); // Output: WHERE name LIKE 'a%' 
  2. before

     $this->db->like('name', 'a', 'before'); // Output: WHERE name LIKE '%a' 
  3. both

     $this->db->like('name', 'a', 'both'); // Output: WHERE name LIKE '%a%' 

Check your database connection and database library loaded as well

$this->db->like() in Codeigniter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM