0

i am try to implement a search that but it shows error when I use Single Quotes like (manu's ,ramu's)

When I change my term part like %".$term."% and use back quotes it shows same error.

Query :

 $this->db->select('*');    
 $this->db->from('tbl_doctor');  
 $this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
 $this->db->where("(tbl_doctor.dr_name LIKE `%".$term."%` OR tbl_doctor.district LIKE `%".$term."%` OR tbl_specialisation.spec_specialise LIKE `%".$term."%`OR tbl_doctor.place LIKE `%".$term."%` )");
 $this->db->limit($limit, $offset);

and my error enter image description here

Touheed Khan
  • 2,149
  • 16
  • 26
Shambu
  • 73
  • 1
  • 9

4 Answers4

3

You need escape string before including it inside query string. Use $this->db->escape_like_str() to escape string.

As you asked "why?", here is the explanation.

Explanation : When you are trying to add anu's in your search query its single quote(') is getting treated as end of string. escape_like_str() will automatically add slash() before any quote to prevent string from unintended termination. Escape That's why you need to escape the string before adding it inside your query.

$this->db->select('*');    
$this->db->from('tbl_doctor');  
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(tbl_doctor.dr_name LIKE '%".$this->db->escape_like_str($term)."%' OR tbl_doctor.district LIKE '%".$this->db->escape_like_str($term)."%' OR tbl_specialisation.spec_specialise LIKE '%".$this->db->escape_like_str($term)."%'OR tbl_doctor.place LIKE '%".$this->db->escape_like_str($term)."%' )"); 
$this->db->limit($limit, $offset);
Touheed Khan
  • 2,149
  • 16
  • 26
1

You need to use Escape String to search. Try this below code with codeigniter function

$this->db->escape_like_str($term)

New Code is

$this->db->select('*');    

$this->db->from('tbl_doctor');  

$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');

$this->db->where("(tbl_doctor.dr_name LIKE `%".$this->db->escape_like_str($term)."%` OR tbl_doctor.district LIKE `%".$this->db->escape_like_str($term)."%` OR tbl_specialisation.spec_specialise LIKE `%".$this->db->escape_like_str($term)."%`OR tbl_doctor.place LIKE `%".$this->db->escape_like_str($term)."%` )"); 

$this->db->limit($limit, $offset);
BEingprabhU
  • 1,618
  • 2
  • 21
  • 28
1

You need to escape the string. Use $this->db->escape_like_str().

Try this code :

$this->db->escape_like_str($term);
Ravi Sachaniya
  • 1,641
  • 18
  • 20
-1

Wrap $term variable with mysql_real_escape_string() in query.

Like : mysql_real_escape_string($term)

Source

Param Bhat
  • 470
  • 1
  • 6
  • 17