我有一个SQL文件,我试图使用以下方法导入我的CodeIgniter应用程序:
public function run_backup($filename) // full extension please
{
// read the file in
// make sure that filename is a string
$filename = (string) $filename;
$backup = $this->load->file($filename, true);
$file_array = explode(';', $backup); // explode on semicolon
foreach($file_array as $query)
{
$this->db->query("SET FOREIGN_KEY_CHECKS = 0");
$this->db->query($query);
$this->db->query("SET FOREIGN_KEY_CHECKS = 1");
}
}
我在sql文件的一半时收到以下错误:
Error Number: 1064
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 ''Mistakes with high risk medicines are much more common than with any other medi' at line 1
INSERT INTO quiz_question_content (`quiz_question_content_id`, `quiz_question_id`, `content`, `image_path`, `type`) VALUES (247, 235, 'Mistakes with high risk medicines are much more common than with any other medicines used
我搜索了该行,发现文本字段中有分号:
'Mistakes with high risk medicines are much more common than with any other medicines used; hence the reason they require special safeguards'
分号正在终止该行并创建格式错误的查询,因为我的方法基于';'
进行拆分
无论如何我可以忽略这个分号吗?即使是逃脱的分号也有分号
;
。 SQL文件非常大,并且该人将未转义的字符放入文本字段中。
答案 0 :(得分:1)
我们在谈论几个问题?我宁愿使用行mysql函数来执行多个quires。 例如http://php.net/manual/en/mysqli.multi-query.php
答案 1 :(得分:0)
分号没有错。您的数据中必须有单/双倒数逗号,由于此错误会发生。
尝试使用mysql_real_escape_string()函数转义数据。 这将为您提供帮助。