转义codeigniter SQL查询 [英] Escaping SQL queries in Codeigniter

查看:218
本文介绍了转义codeigniter SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我插入一些数据使用codeIgniter一个MySQL表。因为我使用 INSERT INTO IGNORE 和不想要编辑的活动记录类启用此功能,我手动生成的SQL查询。

I am inserting some data into a MySQL table using CodeIgniter. Because I am using INSERT IGNORE INTO and do not want to edit the active records class to enable this feature, I am generating the SQL query manually.

$this->db->query("INSERT IGNORE INTO my_table(lat, lng, date, type)
                        VALUES ('" . $data['lat'] . "', '" . $data['lng'] . "', '" . $data['date'] . "', '" . $data['type'] . "')");

问题:查询失败时,在字符串中的 $数据['类型'] 包含一个单引号。我怎样才能让这样的,这些字符需要转义被使用活动记录时自动逃跑了,怎么样?

Problem: The query failed when the string in $data['type'] contained a single quote. How can I make it such that these characters that need to be escaped gets escaped automatically, like when using Active records?

推荐答案

另一种方法是使用查询绑定自动转义所有的值:

Another way is to use Query Binding which automatically escapes all the values:

$sql = "INSERT IGNORE INTO my_table(lat, lng, date, type) VALUES (?,?,?,?);"; 
$this->db->query($sql, array($data['lat'], $data['lng'], $data['date'], $data['type']));

这篇关于转义codeigniter SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆