CodeIgniter Active Record删除特殊字符

时间:2012-06-13 13:53:38

标签: php mysql database codeigniter encoding

我是CodeIgniter的新手,并且在MySQL数据库中插入特殊字符时遇到了问题。 我使用以下代码将数据插入数据库:

return $this->db->insert('table_name', $data);

当我在数据库中查看文本时,所有'''符号都变为'?'迹象。此外,还有一个带有'á'的句子,它被删除了,之后的所有文本都没有插入数据库。

我在插入之前在 $ data 上做了一个var_dump,字符是正确的。

该字段的MySQL排序规则为 latin1_swedish_ci 。 我将其更改为 utf8_general_ci 以查看它是否会有所不同,但后来“€”符号被完全省略。


: 在db插入之前,将charset设置为 latin1

$this->db->query("SET NAMES 'latin1'");
return $this->db->insert('table_name', $data);

另一种解决方案是更改CodeIgniter数据库文件( config / database.php )。代码应为:

$db['default']['char_set'] = 'latin1';
$db['default']['dbcollat'] = 'latin1_swedish_ci';

1 个答案:

答案 0 :(得分:3)

config / database.php文件中配置的charset是什么? See here for info on database config file.

编辑:

另请查看此definitive guide to UTF-8 and CI