【发布时间】:2012-05-29 02:35:13
【问题描述】:
我在执行从文本文件加载的插入命令时遇到问题。我使用 codeIgniter "file" helper 加载一个 sql 行,然后我执行一个简单的 db->query(content of my file)。问题是当从文件加载 sql 时,特殊字符会修剪字符串的其余部分。
这是一个有效的例子
INSERT INTO test(test) VALUES("<p>There is <strong>no special character</strong> in this string</p>");
不起作用的示例
INSERT INTO test(test) VALUES("<p>this character <em>é</em> is a <strong>special character</strong></p>");
在第二个例子中,只会保存"<p> this character <em>"。这很奇怪,因为如果我在 phpMyAdmin 中执行同一行,它就可以正常工作。
有人知道为什么会这样吗?还是我做错了什么?
谢谢
这是一个简单的“重现步骤”。
简单表格:
CREATE TABLE `test` (
`test` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
) ENGINE = InnoDB;
一个文件“application/view/text.txt”包含:
INSERT INTO test(test) VALUES("<p>this character <em>é</em> is a <strong>special character</strong></p>");
我用来执行插入的代码
$this->load->helper('file');
$loaded_sql = read_file(BASEPATH . "../application/views/test.txt");
$this->db->query($loaded_sql);
我的数据库配置
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
CI 配置
$config['charset'] = 'UTF-8';
【问题讨论】:
-
您的文本文件是 UTF8 编码的吗?
-
当然我没有检查。文件编码为 ANSI(我猜是默认记事本编码)。我将文件保存为 UTF-8,但它似乎不起作用。这是 CI 显示的错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO test(test) VALUES(" this character é is a spe' at line 1
标签: database codeigniter character-encoding insert