CodeIgniter' where'不工作

时间:2014-04-26 15:17:31

标签: php mysql sql codeigniter

这是我制作的搜索功能,它获取搜索字词并显示正常。 HM_Jobs JobStatus提供了3个选项: OpenAcceptedComplete

然而,当我运行时,搜索也会从AcceptedComplete行中提取结果,为什么WHERE语句不会阻止这种情况发生?

function search_jobs($search) //This function checks a search term against the job title and description.
{
    $this->db->select('*');
    $this->db->from('HM_Jobs');
    $this->db->like('JobTitle',$search);
    $this->db->or_like('JobDescription',$search);
    $this->db->where('JobStatus','Open');  

    $query = $this->db->get();
    return $query->result(); 
}

2 个答案:

答案 0 :(得分:1)

试试这个

 $this->db->select('*');
 $this->db->from('HM_Jobs');
 $this->db->where("(JobTitle LIKE '$search' OR JobDescription LIKE '$search')"  );
 $this->db->where('JobStatus','Open'); 
 $query = $this->db->get();

您可以使用echo $this->db->last_query()

查看您的查询是否已执行

您的查询就像

一样
where JobTitle like 'search' or JobDescription  like 'search' and JobStatus='Open'

但您需要的查询需要类似

的内容
  where (JobTitle like 'search' or JobDescription  like 'search') and JobStatus='Open'

答案 1 :(得分:1)

尝试使用以下查询

function search_jobs($search)
{
    $query = $this->db->select('*')
                  ->from('HM_Jobs')
                  ->where('JobStatus','Open')
                  ->where("(`JobTitle` LIKE '%$search%' OR 
                            `JobDescription` LIKE '%$search%')",null,false)
                  ->get();
    return $query->result(); 
}