简体   繁体   中英

Loading CSV file into MYSQL database not importing rows

I have the following function:

    function bulk_insert_file($filename) {

        $file_location = 'assets/temp/'.$filename;

        $sql = 'LOAD DATA LOCAL INFILE '."'$file_location'".' INTO TABLE p4p.users_csv_import
            FIELDS TERMINATED BY \',\'
            LINES TERMINATED BY \'\\r\\n\'
            IGNORE 1 LINES';
        $this->db->query($sql);
    }

And the following CSV file:

,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,

However, when I run the code no data is imported. If I remove the IGNORE 1 LINES part I will at least get the first row imported.

CREATE TABLE `users_csv_import` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `role_id` int(11) NOT NULL DEFAULT '1',
  `unique_id` varchar(255) DEFAULT '',
  `first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `last_name` varchar(50) CHARACTER SET latin1 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `company` int(11) NOT NULL DEFAULT '0',
  `nonprofit` int(11) NOT NULL,
  `username` varchar(100) CHARACTER SET latin1 NOT NULL DEFAULT '',
  `password` varchar(34) CHARACTER SET latin1 NOT NULL DEFAULT '',
  `dimension_data` text,
  `raw_csv_data` text,
  PRIMARY KEY (`id`)
 )ENGINE=InnoDB DEFAULT CHARSET=utf8;

Here is the specs on the table..

I assume the csv file is in unix line terminators format. You should therefore try importing with LINES TERMINATED BY \\'\\\\n\\' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM