Codeigniter this-> db->查询

时间:2012-10-13 20:36:42

标签: php codeigniter

$ this-db-> query()是否具有mysql注入保护?我想知道因为我在实例中使用它并且没有做任何事情来防止sql注入。

3 个答案:

答案 0 :(得分:5)

使用CodeIgniter查询的ActiveRecord样式转义参数,但不转义query()。

您可以这种方式使用活动记录:

$someAge = 25;
$this->db->select('names, age');
$query = $this->db->get_where('people', array('age' => '>' . $someAge));

在此处详细了解:https://www.codeigniter.com/userguide2/database/active_record.html

答案 1 :(得分:4)

不,db-> query()默认情况下不受SQL注入保护,你有几个选项。 使用查询绑定

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; 
$this->db->query($sql, array(3, 'live', 'Rick'));

对于你必须在继续构建查询的更复杂的quires,使用compile_bind()来获取SQL块。

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; 
$safe_sql  = $this->db->compile_bind($sql, array(3, 'live', 'Rick'));

或者在参数

上使用escape $ this-> db-> escape()
$sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";

最好先使用表单验证,然后将xss_clear,max_length等内容与上述方法结合使用。

答案 2 :(得分:0)

您可以使用query bindings

CI 3用户指南中的示例:

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));
相关问题