codeIgniter ActiveRecord的加盟与反引号()方法包装数 [英] CodeIgniter ActiveRecord join() method wrapping numbers with backticks
问题描述
这是我的活动记录code。在codeIgniter:
$这个 - > DB-> ...
$这个 - > DB->加入('post_likes','post_likes.user_id ='。$这个 - > DB->逃生($ online_user),而post_likes.post_id = post.id', '剩下');
这是它是如何跨preTED:
LEFT JOIN`post_likes`开`post_likes`.`user_id` =`3`和post_likes.post_id = post.id
它提供了错误:
`user_id` =`3`
如何在活动记录写一个直接的数字?
更新:
删除逃跑
到您的计算机,你不需要有一个数据库上进行测试。只是想这code显示了错误:
$这个 - > DB->选择(*)
- 肽从('mytable的')
- >加入('post_likes','post_likes.user_id =3与post_likes.post_id = post.id','左');
$查询= $这个 - > DB->获得('');
的var_dump($这 - > DB-> last_query());
出口(0);
结果:
数据库出错
错误编号:1064
你在你的SQL语法错误;检查对应于你的MySQL服务器版本的权利语法使用近''和post_likes.post_id = post.id'在第3行手册
SELECT * FROM(`mytable`)LEFT JOIN`post_likes`开`post_likes`.`user_id` =`3`和post_likes.post_id = post.id
你不应该使用双引号的SQL查询:
$这个 - > DB->加入('post_likes,post_likes.user_id = $ online_user和post_likes.post_id = post.id,左);
更新:
这是在当前的CI稳定版中的错误(固定在3.0-DEV),CI ActiveRecord的方法(这并没有实现真正的ActiveRecord)的ppared进行简单的用法$ P $。
我之前通过黑客的核心文件,修复了这个问题(通过添加参数加入方法来禁用 _protect_identifires
)。
在那里,我们去:
在系统/数据库/ DB_active_rec.php
线#310,加入 $逃避
作为第4个参数:
公共职能联接($表,$ COND,$类型='',$逃生= TRUE)
和变化 $匹配[3] = ...
来:
如果($逃脱===真)
{
$匹配[3] = $这 - > _protect_identifiers($匹配[3]);
}
所以,你可以使用加入($表,$ COND,$类型='',$逃生= FALSE)
禁用的逃跑的
此外,设置 _protect_identifires
在全球范围内 FALSE
不是一个正确的方向。
唯一的选择仍然是使用自定义的 查询()
:
$ SQL =SELECT * FROM some_table WHERE ID =?
$这个 - > DB->查询($的SQL,阵列(3));
This is my active record code in CodeIgniter:
$this->db->...
$this->db->join('post_likes', 'post_likes.user_id="'.$this->db->escape($online_user).'" AND post_likes.post_id=post.id', 'left');
And this is how it is interpreted:
LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id
it gives the error:
`user_id`="`3"`
How to write a direct number in active record?
Update:
removing escape
to test it on your computer you dont need to have a database. Just trying this code shows the error:
$this->db->select('*')
->from('mytable')
->join('post_likes', 'post_likes.user_id="3" AND post_likes.post_id=post.id', 'left');
$query=$this->db->get('');
var_dump($this->db->last_query());
exit(0);
result:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '` AND post_likes.post_id=post.id' at line 3
SELECT * FROM (`mytable`) LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id
You SHOULD not use the double quotes in SQL query:
$this->db->join('post_likes', "post_likes.user_id = $online_user AND post_likes.post_id=post.id", 'left');
Update:
This is a bug in the current CI stable version (fixed in v3.0-DEV), CI ActiveRecord methods (which doesn't implement really ActiveRecord) are prepared for simple usages.
I fixed this issue before by hacking the core files (by adding a parameter to join method to disable _protect_identifires
).
There we go:
In system/database/DB_active_rec.php
line #310, add $escape
as 4th parameter:
public function join($table, $cond, $type = '', $escape = TRUE)
And change $match[3] = ...
to:
if ($escape === TRUE)
{
$match[3] = $this->_protect_identifiers($match[3]);
}
So, you can use join($table, $cond, $type = '', $escape = FALSE)
to disable escaping.
In addition, setting _protect_identifires
globally to FALSE
is not in a correct direction.
the only option remains is using custom query()
:
$sql = "SELECT * FROM some_table WHERE id = ?"
$this->db->query($sql, array(3));
这篇关于codeIgniter ActiveRecord的加盟与反引号()方法包装数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!