1

我有一个 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' 

分号终止该行并创建格式错误的查询,因为我的方法是基于拆分';'

无论如何我可以忽略这个分号吗?即使是转义的分号也有分号 &#59;。SQL 文件非常大,并且该人已将未转义的字符放入文本字​​段中。

4

2 回答 2

1

我们在谈论多少查询?我宁愿使用行 mysql 函数来执行多个查询。例如http://php.net/manual/en/mysqli.multi-query.php

于 2013-08-22T04:33:20.150 回答
0

分号没有错。您的数据中必须有单/双引号,因此会发生此错误。

尝试使用 mysql_real_escape_string() 函数转义数据。这将对您有所帮助。

于 2019-03-25T16:37:28.217 回答