I have written this CodeIgniter code for an advanced search. this advanced search has only a list of provinced for the uses to select from. But, I have problem, when searching, it brings results from other cities not selected by the user. Here is the code to match for the term:
$db->or_like("title", $_POST['search-term']); // OR
$db->or_like("content", $_POST['search-term']); // OR
$db->or_like("name", $_POST['search-term']); // OR
$db->or_like("keywords", $_POST['search-term']); // OR
And here is part of code responsible for matching with the selected province:
if(isset($_POST['province']) && $_POST['province'] != "0")
{
$db->where("province", $_POST['province']);
if(isset($_POST['cities']) && $_POST['cities'] != "0")
{
$db->where("city", $_POST['cities']);
}
} // AND
And here is the code responsible for getting the data into an array:
$search_result = $db->get("ads")->result_array();
It is also worth mentioning that when I change the or_like
to like
it yields no result.
Here is the generated QUERY:
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
It seems to be sound and OK, but the results are not...
try this query then,
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
Update: in CI you can try it like this:
$sql = "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";
$this->db->query($sql);
this would solve your problem.
Side Notes: your code is saying welcome to SQL injection. be aware of what you are doing.