我试图将mysql查询本身插入到表字段中(当满足某些条件时执行一系列查询),但只要查询中有任何特殊字符,它就会转换为相应的实体。
例如,如果我将此查询插入到表中,则引号将变为"';",>to>;以及空间到&#。是否有任何方法可以按原样插入查询并以正确的形式显示。
"select
case item_id
when 206 then '1 Column'
when 255 then '2 Columns'
end as split,
# case oi.product_id
# when 24 then 'XXXX'
# when 28 then 'CCCC'
# when 30 then 'EEEE'
# else 'Something Else'
# end as product,
case oi.price_id
when 72 then 'UYT - Single Pay'
when 73 then 'UYT - Single Pay'
when 74 then 'UYT - Single Pay'
else 'Upsell'
end as product,
count(distinct(al.cust_id)) as the_count
from logtable al
where item_id in (206,255) and
activity_dts > '2012-01-31 19:15:00' and
o.order_is_refunded = 0 and
t.response_code = 1 and
t.response_reasontext not like '%testmode%'
group by 1,2;"
请给我建议,否则我会遗漏什么吗。CI安装使用的字符集是UTF-8。
插入字符串应该没有任何问题,因为它是字符串,而且数据库库本身不进行任何字符编码,afaik。数据库和表是什么编码?(根据默认设置,连接应为UTF-8)
由于您使用的是该框架,因此可以使用它的方法(并且而不是mysql_real_eescape_string(),或者,天哪,addslashes()!!如其他答案中所建议的)。这应该有效:
$string_query = "select
case item_id
when 206 then '1 Column'
when 255 then '2 Columns'....";
$sql = "INSERT INTO table(column) VALUES (?)";
$this->db->query($sql, array($string_query));
查询绑定会自动转义FOR SQL,因此您是安全的(您本可以使用具有相同结果的ActiveRecord)。我不知道什么东西不能在这里工作,当然没有函数编码html。
也许您在其他地方进行编码:您确定没有调用xss_clean()
、htmlspecialchars()
/htmlentities()
,或者您启用了XSS保护,或者您将TRUE作为$this->input->
的第二个参数传递?
如果由于某种原因——因为你没有提供足够的信息——以上所有操作都失败了,你可以随时对所有内容进行编码:
$string_query = base64_encode("select.....");
// HERE YOU MAKE THE INSERT QUERY
当您检索它时,您base64_decode()
字符串。但理想的解决方案不是这样,它应该毫无瑕疵地工作。
PHP插件
此函数将帮助您在数据库中按原样添加查询及其引号。
你也可以这样做,
如果你的查询只有多个单引号,那么用双引号将其括起来:
如$tmp = "See single' quotes";
反之亦然,如:
$tmp = 'See double " quotes';
尝试以下
$data_insert = mysql_real_escape_string($values);
你也可以找到它的详细信息http://php.net/manual/en/function.mysql-real-escape-string.php