Conny Olsson
Conny Olsson

Reputation: 1577

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.

Upvotes: 0

Views: 134

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

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

Upvotes: 1

Related Questions