Codeigniter Active Recordを使用してMySQLテーブルの単一フィールドで複数の値を検索する方法は?

StackOverflow https://stackoverflow.com/questions/19853225

質問

CodeigniterでMySQLを使用して、フィールドで複数の値を検索する必要があります。これが私のコードに従います。

コントローラーで

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

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

}

モデルで

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');
  }
}

ビューで つまり、(../Employer/Result)

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

しかし、「スキル」を検索するフィールドではなく、「JS_EDU_DETAILS」テーブルのすべてのレコードを取得しています。

どこで間違っているの?あらゆる助けに感謝し、事前に感謝します。

役に立ちましたか?

解決

試す:

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 声明

他のヒント

このようにコードを適切に配置する必要があります

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top