如何使用codeigniter活动记录在mysql表的单个字段中搜索多个值?

时间:2013-11-08 06:53:53

标签: php mysql codeigniter active-record-query

我必须在codeigniter中使用mysql在字段中搜索多个值。以下是我的代码。

在控制器中

public function vpsearch()
{
  $data['info'] = $this->psearch_m->emp_search_form();

  $this->load->view("employer/result",$data);       

}

IN模型

public function emp_search_form()
{
  $skill = $this->security->xss_clean($this->input->post('ps_skills'));
  $jrole = $this->input->post('ps_jobrole'));


  if ( $jrole !== NULL) 
  {
    return $this->db->get('js_edu_details');
    $this->db->like('js_skills','$skill');
  }
}

在视野中即,(../雇主/结果)

foreach($info->result() as $row)
{
  echo $row->js_id."<br/><br/>" ;
}

但是我在“js_edu_details”表中获取所有记录,而不是搜索“技能”的字段。

我哪里错了?任何帮助wud b赞赏,thanx提前。

2 个答案:

答案 0 :(得分:0)

尝试:

public function emp_search_form()
{
    $skill = $this->security->xss_clean($this->input->post('ps_skills'));
    //$skill = $this->input->post('ps_skills', true);  other short way of getting the above result with `xss clean`
    if ( $jrole !== NULL) 
    {
        $this->db->like('js_skills',$skill); #remove the single quote around the `$skill`
        $res = $this->db->get('js_edu_details');
        echo $this->db->last_query(); #try to print the query generated
        return $res;
    }
}

Return语句应在like语句

之后

答案 1 :(得分:0)

您应该像这样正确安排代码

public function emp_search_form()
{
    $ps_skills  =   $this->input->post('ps_skills')
    $skill      = $this->security->xss_clean($ps_skills);

    if ( $jrole !== NULL) 
    {
        $this->db->like('js_skills','$skill');
        return $this->db->get('js_edu_details');
    }
}

另外你应该注意病情永远不会满足。它总是会给出错误undefined variable $jrole