shadowhung 发表于 2012-4-19 11:43:38

Active Record 类 的疑问

$sql="select count(*) from table where a=a and b=d and(c like %a% and d like %a%)"; 这样的SQL语句用CI的Active Record 类 来写该怎么写 中间的and(c like %a% and d like %a%)";

$this->db->where(array('a'=>a,'b'=>b));
$this->db->where($this->db->or_like('c'=>a,'d'=>d));

像这样子吗?

Hex 发表于 2012-4-19 11:53:09

复杂语句不建议用 AR 来写。

shadowhung 发表于 2012-4-19 11:57:47

嗯 在搜索那块有碰到个问题是 当输入    '"      到搜索框时 就会暴露MYSQL 错误原来的程序就是 用SQL 来写的'accname LIKE "%'.addslashes($keyWords).'%" OR accdesc LIKE "%'.addslashes($keyWords).'%")'像这样

Hex 发表于 2012-4-19 12:09:56

输入内容要转义,CI 都提供转义函数了。
看这里 http://codeigniter.org.cn/user_guide/database/queries.html

暗夜星辰 发表于 2012-4-19 12:22:45

转义查询

将数据转义以后提交到你的数据库是非常好的安全做法,CodeIgniter 提供了 3 个函数帮助你完成这个工作。

$this->db->escape() 这个函数将会确定数据类型,以便仅对字符串类型数据进行转义。并且,它也会自动把数据用单引号括起来,所以你不必手动添加单引号,用法如下:
$sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";
$this->db->escape_str() 此函数将忽略数据类型对传入数据进行转义。更多时候你将使用上面的函数而不是这个。这个函数的使用方法是:
$sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')";
$this->db->escape_like_str() This method should be used when strings are to be used in LIKE conditions so that LIKE wildcards ('%', '_') in the string are also properly escaped.
$search = '20% raise';
$sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";

shadowhung 发表于 2012-4-21 17:34:56

跨站请求伪造攻击漏洞 这是什么? 网站检测有这项问题 该如何防范?
页: [1]
查看完整版本: Active Record 类 的疑问