【问题标题】:codeigniter : searching string with quotes( ' or " ) showing errorcodeigniter:搜索带引号('或“)的字符串显示错误
【发布时间】:2018-07-19 19:56:40
【问题描述】:

我正在尝试实现一个搜索,但是当我使用单引号时显示错误,例如 (manu's ,ramu's)

当我更改我的术语部分(如 %".$term."%)并使用反引号时,它会显示相同的错误。

查询:

 $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);

还有我的错误

【问题讨论】:

  • 使用 mysql_real_escape_string($term) 并尝试
  • 使用$this->db->escape_like_str()转义字符串。

标签: php mysql database codeigniter search


【解决方案1】:

在将其包含在查询字符串中之前,您需要转义字符串。使用 $this->db->escape_like_str() 转义字符串。

正如你问“为什么?”,这里是解释。

解释:当您尝试在搜索查询中添加 anu's 时,其单引号 (') 被视为字符串结尾。 escape_like_str() 将在任何引号之前自动添加 slash() 以防止字符串意外终止。转义 这就是为什么您需要在将字符串添加到查询中之前对其进行转义。

$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);

【讨论】:

  • 单引号问题
  • 很高兴听到这个消息,投赞成票并接受,以便其他用户可以轻松找到解决方案。
【解决方案2】:

您需要使用转义字符串进行搜索。用codeigniter函数试试下面的代码

$this->db->e​​scape_like_str($term)

新代码是

$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);

【讨论】:

    【解决方案3】:

    在查询中使用 mysql_real_escape_string() 包装 $term 变量。

    喜欢:mysql_real_escape_string($term)

    Source

    【讨论】:

      【解决方案4】:

      您需要对字符串进行转义。使用$this->db->escape_like_str()

      试试这个代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2011-06-11
        • 1970-01-01
        • 2016-09-02
        • 2014-01-17
        相关资源
        最近更新 更多