就像codelgniter中的查询字符串一样

时间:2015-04-25 10:39:21

标签: codeigniter search

我的视图页面包含以下字段

id,name,email,gender etc..

我有一个搜索框,我想在以上所有字段中执行搜索

如何以CI方式编写查询字符串

控制器

public function search()
    {


        $sql = "SELECT * FROM users";

        $this->form_validation->set_rules('search_term', 'Search_term', 'trim|xss_clean');

        if ($this->form_validation->run() == TRUE)
        {
             $keyword = $this->input->post('search_term');

             $sql.= "WHERE name like '{$keyword }'";
             $sql.= "OR email like '{$keyword }'"; 
             $sql.= "OR gender like '{$keyword }'";          

        }

        $query = mysql_query($sql);
    }  

1 个答案:

答案 0 :(得分:5)

阅读CI Like之类的查询

    $keyword = $this->input->post('search_term');
    $this->db->select('*');
    $this->db->like('name', $keyword);
    $this->db->or_like('email', $keyword);
    $this->db->or_like('gender', $keyword);
    $res = $this->db->get('users');