views:

22

answers:

1

Hey.. I've run into a problem using several "like" in my sql-query (generated with Codeigniter's activerecord):

SELECT * FROM (`posts`) WHERE `city` LIKE '%test%' AND `title` LIKE '%%' OR `text` LIKE '%%'

Thing is, it appears to read this query as if there was a parenthesis around the first like and the second like, but I want there to be a parenthesis around the second and the last like (I want it to compare if the last or the next to last works).

How can I achieve this using Codeigniter's Active Record class?

Current code: if($type != 0) $this->db->where('type', $type);

    $this->db->like('city', $area);
    $this->db->like('title', $words);
    $this->db->or_like('text', $words);

    return $this->db->get('posts')->result_array(); 
A: 

I don't think CI is adding any paranthesis. It will add an 'AND' between the first two statements and an 'OR' between the last two. To achieve what you want, I would write my own statement. It's very straightforward.

$sql = SELECT * FROM 'posts' WHERE city LIKE ? AND ( title LIKE ? OR text LIKE ? );
$query = $this->db->query($sql, array($area, $words, $words));

Notice how I've used binding. This automatically escapes characters for you.

musoNic80
Ok, thx this will solve my problem. You are right, I don't think I can add parenthesis with Codeigniter's active record.
Calle