Mahdi Majidzadeh
Mahdi Majidzadeh

Reputation: 868

Codeigniter - use two like and where together

I have a problem with use two like statement and where together:

    $this->db->select('something');
    $this->db->where('WHERE',$var);
    $this->db->where_in('WHEREIN', $var);
    $this->db->like('LIKE1',$query);
    $this->db->or_like('LIKE2',$query);
    $query = $this->db->get('table');

My query must select LIKE1 or LIKE2 where WHERE andWHEREIN is true.

If I use or_like, where statement get or too,

If i use just like, it's become like AND like statement

Any solution??

Upvotes: 5

Views: 3873

Answers (3)

Mahdi Majidzadeh
Mahdi Majidzadeh

Reputation: 868

I found this solution:

use group_start() and group_end(), so my code turn to

$this->db->select('something');
$this->db->where('WHERE',$var);
$this->db->where_in('WHEREIN', $var);
$this->db->group_start();
$this->db->like('LIKE1',$query);
$this->db->or_like('LIKE2',$query);
$this->db->group_end();
$query = $this->db->get('table');

Upvotes: 5

davidev
davidev

Reputation: 314

When you run this kind of query is better if you create a model like this:

function example(){

    $query = "( SQL QUERY )";
    $search = $this->db->query($query);
    return $search->result_array(); 
}

Upvotes: 0

Joerg
Joerg

Reputation: 3101

If you have a complex where clause, you can write it this way:

$this->db->where("doc = '123456' AND place IN('1,2,3') AND name (LIKE '%query%' ESCAPE '!' OR code LIKE '%query%' ESCAPE '!' )", NULL);

Upvotes: 0

Related Questions