php - 奇怪的 Codeigniter 查询转义

标签 php mysql codeigniter-2

问题是当字符串包含单词“or”和“like”的组合时,事件记录语句中的字符串被转义。这是我使用的代码

$this->db->select('id');
$this->db->where('phrase', $phrase);
$this->db->from('phrases');
$ret    = $this->db->get();

这适用于普通字符串,但当 $phrase 遇到像这样的字符串时失败 “点击您要使用的事件或预订代码。” 其中“或”和“喜欢”作为单词出现。

查询结果如下

SELECT `id` FROM `phrases` WHERE `phrase` = 'Click on the Event or `Booking Code you` `would` like to use.'

我不能禁用转义,因为这对其他字符串来说很方便。

最佳答案

我认为您需要在 where 子句中添加第三个参数来转义值。

$this->db->select('id');
$this->db->where('phrase', $phrase, FALSE);
$this->db->from('phrases');
$ret    = $this->db->get();

来自manual :

where($key[, $value = NULL[, $escape = NULL]])

Parameters:
$key (mixed) – Name of field to compare, or associative array

$value (mixed) – If a single key, compared to this value

$escape (bool) – Whether to escape values and identifiers

有关 Stack overflow 的这篇文章中有更多信息.

关于php - 奇怪的 Codeigniter 查询转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38222291/

相关文章:

MySQL - 插入后更新同一个表的触发器

php - 如何使用 PHP 远程连接到 OpenShift MySQL 数据库?

php - 通过curl登录并重用cookie进行第二个请求

php - 如何向未登录用户显示独特的内容?

java - 计算距离 - 即时计算还是在数据输入数据库后立即计算?

mysql - 无法将列数据从 MySQL 中的一个表复制到另一个表

php - 如何在codeigniter中定义全局变量?

php - Codeigniter 查询结果返回带有 setter 的自定义结果对象

sql-injection - $this->input->xss_clean($data) 使用 codeigniter 给出 fatal error

php - 来自测试方法正在使用的同一类的模拟方法