【问题标题】:How to search for multiple values in a single field of mysql table using codeigniter active record?如何使用codeigniter活动记录在mysql表的单个字段中搜索多个值?
【发布时间】:2013-11-08 06:53:53
【问题描述】:

我必须在 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');
  }
}

考虑即(../employer/result)

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

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

我哪里出错了?任何帮助 wud b 感谢,thanx 提前。

【问题讨论】:

    标签: php mysql codeigniter active-record-query


    【解决方案1】:

    试试:

    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 语句之后

    【讨论】:

    • 试过你的建议,没有改变。获取表中的所有记录,而不考虑 $skills 中的值。
    • 什么是$jrole !== NULL?你从哪里得到这个?编辑了上面的代码再试一次。
    • Thanx Nil'z ,发现错误!!我忘了关闭 在视图中。这就是为什么无法读取 $this->db->like('js_skills',$skill); 中的 $skill 值的原因。 .. !!我在打印查询后才发现这个......非常感谢你的帮助。!!
    • 乐于助人,如果对您有帮助,请考虑接受答案。
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2013-06-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2019-12-08
      相关资源
      最近更新 更多