I search a word from my DB using model in codeigniter. I done the search for exact word searching. but I need also find a word when I give partial word. I use LIKE clause for search a word. But I don't know how to use LIKE% clause in my model function.
我使用codeigniter中的模型从我的数据库中搜索一个单词。我完成了搜索确切的单词搜索。但是当我给出部分词语时,我也需要找到一个词。我使用LIKE子句搜索一个单词。但我不知道如何在我的模型函数中使用LIKE%子句。
Model:
模型:
public function get_search() {
$match = $this->input->post("search");
$match1 = $this->input->post("search1");
$match2 = $this->input->post("search2");
$match3 = $this->input->post("search3");
$match4 = $this->input->post("search4");
$match5 = $this->input->post("search5");
$match6 = $this->input->post("search6");
$this->db->query("SELECT * FROM patient_creation;");
$this->db->where("patient_id like '$match' OR mobile like '$match1' OR name like '$match2' OR fathername like '$match3' OR address like '$match4' OR created BETWEEN '$match5' AND '$match6'");
//$this->db->or_where("name LIKE '".$match2."%'");
$query = $this->db->get("patient_creation");
return $query->result();
}
I need LIKE% search for $match2, $match3 and $match4 variables.
我需要LIKE%搜索$ match2,$ match3和$ match4变量。
3 个解决方案
#1
0
Change :
变化:
$this->db->where("patient_id like '$match' OR mobile like '$match1' OR name like '$match2' OR fathername like '$match3' OR address like '$match4' OR created BETWEEN '$match5' AND '$match6'");
to
至
$this->db->where("patient_id like '".$match."' OR mobile like '".$match1."' OR name like '".$match2."' OR fathername like '".$match3."%' OR address like '".$match4."%' OR created BETWEEN '".$match5."' AND '".$match6."'");
#2
0
Note the semicolon in SELECT * FROM patient_creation;
prepare query string and use in query func like,
注意SELECT * FROM patient_creation中的分号;准备查询字符串并在查询函数中使用,如,
$query = "SELECT * FROM patient_creation where patient_id like '$match' OR mobile like '$match1' OR name like '$match2' OR fathername like '$match3' OR address like '$match4' OR created BETWEEN '$match5' AND '$match6'";
$this->db->query($query);
#3
0
public function get_search() {
$match = $this->input->post("search");
$match1 = $this->input->post("search1");
$match2 = $this->input->post("search2");
$match3 = $this->input->post("search3");
$match4 = $this->input->post("search4");
$match5 = $this->input->post("search5");
$match6 = $this->input->post("search6");
$this->db->select("*");
$this->db->like("patient_id" ,$match);
$this->db->or_like("name" ,$match2);
$this->db->or_like("fathername" ,$match3);
$this->db->or_like("address" ,$match4);
$this->db->or_like("name" ,$match2);
$this->db->where('created >=', $match5);
$this->db->where('created <=', $match6);
$query = $this->db->get("patient_creation");
return $query->result();
}
#1
0
Change :
变化:
$this->db->where("patient_id like '$match' OR mobile like '$match1' OR name like '$match2' OR fathername like '$match3' OR address like '$match4' OR created BETWEEN '$match5' AND '$match6'");
to
至
$this->db->where("patient_id like '".$match."' OR mobile like '".$match1."' OR name like '".$match2."' OR fathername like '".$match3."%' OR address like '".$match4."%' OR created BETWEEN '".$match5."' AND '".$match6."'");
#2
0
Note the semicolon in SELECT * FROM patient_creation;
prepare query string and use in query func like,
注意SELECT * FROM patient_creation中的分号;准备查询字符串并在查询函数中使用,如,
$query = "SELECT * FROM patient_creation where patient_id like '$match' OR mobile like '$match1' OR name like '$match2' OR fathername like '$match3' OR address like '$match4' OR created BETWEEN '$match5' AND '$match6'";
$this->db->query($query);
#3
0
public function get_search() {
$match = $this->input->post("search");
$match1 = $this->input->post("search1");
$match2 = $this->input->post("search2");
$match3 = $this->input->post("search3");
$match4 = $this->input->post("search4");
$match5 = $this->input->post("search5");
$match6 = $this->input->post("search6");
$this->db->select("*");
$this->db->like("patient_id" ,$match);
$this->db->or_like("name" ,$match2);
$this->db->or_like("fathername" ,$match3);
$this->db->or_like("address" ,$match4);
$this->db->or_like("name" ,$match2);
$this->db->where('created >=', $match5);
$this->db->where('created <=', $match6);
$query = $this->db->get("patient_creation");
return $query->result();
}