CodeIgniter生成的Statement没有按预期给出结果

I have printed out a CI generated code, so to talk about the statement, since it would be easier. Here it is:

SELECT * 
FROM (`ads`) 
WHERE `status` = 2 AND `province` = '5' AND `title` LIKE '%شریف%' 
      OR `content` LIKE '%شریف%' OR `name` LIKE '%شریف%' 
      OR `keywords` LIKE '%شریف%' 
ORDER BY `stars` DESC

But, it shows results which are with value "8" for the province, while it is said in statement to only get results with province "5". Why it does not work correctly?

The operator and has a higher precedence than the operator or. That means that if you query:

select  *
from    applicants
where   expected_salary < 10
        or knowledge = 'high' 
        and has_residency_permit = 'true'

you could get someone with a low salary without a residency permit. The solution is to use parenthesis to override precedence:

select  *
from    applicants
where   (expected_salary < 10
        or knowledge = 'high')
        and has_residency_permit = 'true'

This query will only return applicants with a residency permit.

You may try this (CI supports Custom string)

$where = "title LIKE '%شریف%' OR content LIKE '%شریف%' OR name LIKE '%شریف%' OR keywords LIKE '%شریف%'";
$this->db->select('*');
$this->db->from('ads');
$this->db->where($where);
$this->db->where("status = 2 AND province = '5'");
$this->db->order_by('stars', 'desc');

Go to the given link above and look for custom string.