将数据从csv导入mysql

时间:2014-12-09 16:28:47

标签: php mysql codeigniter csv

嘿伙计们,我是codeigniter的新手。我在这个项目中使用codeigniter。 我一直在尝试上传.csv文件并将其保存到我的桌面。但它给了我一个数据库错误。 我正在使用php的codeigniter框架。

模型:

  public function save_csv($data)
        {
            $infile=$data['upload_data']['full_path'];
            print_r($infile);
            $query = $this->db->query("LOAD DATA INFILE '".$infile."'
                    INTO TABLE promotion_product
                    FIELDS TERMINATED BY ','
                    ESCAPED BY '\\' 
                    LINES TERMINATED BY '\r\n'
                    IGNORE 1 LINES
                    ");


            print_r($query);
            return $query;
        } 

控制器:

  public function upload() {
            $image_path = realpath(APPPATH . '../uploads');
             //$file_path =  $image_path.$file_data['file_name'];
            $config['upload_path'] = $image_path;
            $config['allowed_types'] = '*';
            $config['max_size'] = '100';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';

            $this->load->library('upload', $config);

            if (!$this->upload->do_upload()) {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('upload_form', $error);
            } else {

                $data = array('upload_data' => $this->upload->data());

                //$pramod=$data[upload_data][full_path];
                //print_r($pramod);
                $this->load->view('upload_success', $data);
                $this->usermodel->save_csv($data);
            }
        }

这是给我的错误:

A Database Error Occurred

    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 'C:/wamp/www/mapApplication/uploads/pramotion113.csv INTO TABLE ' at line 1

LOAD DATA INFILE C:/wamp/www/mapApplication/uploads/pramotion113.csv INTO TABLE promotion_product FIELDS TERMINATED BY ',' ESCAPED BY '\' LINES TERMINATED BY ' ' IGNORE 1 LINES

Filename: C:\wamp\www\mapApplication\system\database\DB_driver.php

Line Number: 330

1 个答案:

答案 0 :(得分:1)

SequelProPHPmyAdmin这样的工具可以轻松完成此操作,或者在CodeIgniter中是否有特定的理由?

编辑:

这篇文章似乎有很多与您尝试实现的内容相关的信息: PHP - Import CSV file to mysql database Using LOAD DATA INFILE