load data infile is not working - php

I am trying to batch upload by csv file. my csv file located in webroot/CSV/data.csv. Data are as follows:
name,email
santo, abc#gmail.com
my code:
$sql = "LOAD DATA INFILE '$filepath'
INTO TABLE customers
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY ',,,\\r\\n'
IGNORE 1 LINES
(name, email)";
$this->Customer->query($sql);
here
$filepath = WWW_ROOT . 'CSV' . DS.'data.csv';
Getting error:
C:\server\mysql\data\serverhtdocsdemo-home25appwebrootCSVdata.csv' not found (Errcode: 2 "No such file or directory")
I check the file it exists and file location in sql seems to okay:
SQL Query: LOAD DATA INFILE 'C:\server\htdocs\demo-home25\app\webroot\CSV\data.csv' INTO TABLE customers FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',,,\r\n' IGNORE 1 LINES (name, email)
What is the wrong in my code?

You tried to use "LOAD DATA LOCAL INFILE"?
LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3, col4, col5...);
In your case:
$sql = "LOAD DATA LOCAL INFILE '$filepath'
INTO TABLE customers
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY ',,,\\r\\n'
IGNORE 1 LINES
(name, email)";
$this->Customer->query($sql);
"If LOCAL is specified, the file is read by the client program on the client host and sent to the server." from Documentation
Answered in https://stackoverflow.com/a/14133740/2925795

I had the same issue. Sometimes it happens that your mysql server and client understand file locations differently.
Add LOCAL modifier (as LOAD DATA LOCAL INFILE) to your query
$sql = "LOAD DATA LOCAL INFILE '$filepath'
INTO TABLE customers
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY ',,,\\r\\n'
IGNORE 1 LINES
(name, email)";
$this->Customer->query($sql);

Related

PHP - Rows are not in proper format after importing csv to mysql using LOAD INTO INFILE

I'm new here. I would like to ask question regarding with my codes. So my code is working perfectly but then after importing csv to mysql programmatically using LOAD INTO INFILE. I didn't know why my output kept getting this kind of format. Please see my codes below thanks!
$testing = $conn->prepare("LOAD DATA INFILE 'C:/xampp/mysql/data/mysql/usagetable.csv'
INTO TABLE trieinitialcountentry
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(Count_ID, InvItemLocID, OnHand, OnHandValue, StockCenter_ID)");
$testing->execute();
And also, some rows had been imported perfectly but some of them are not. Example of my output:
Count_ID InvItemLocID OnHand OnHandValue StockCenter_ID
737450 -2091889269 140.00 "2 788.80"
Apparently some of the values in the input CSV file are wrapped in quotes and you didn't pass this information to LOAD DATA INFILE.
Try this query:
$query = <<<'END'
LOAD DATA INFILE 'C:/xampp/mysql/data/mysql/usagetable.csv'
INTO TABLE trieinitialcountentry
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(Count_ID, InvItemLocID, OnHand, OnHandValue, StockCenter_ID)
END;
$testing = $conn->prepare($query);
$testing->execute();
Note the added OPTIONALLY ENCLOSED BY '"' clause.

mysql Load data local infile for one csv fields terminated by , and for another csv field terminated by ;

I already written a query i have posted here but that is working with similar kinds of csv.I need a load data local infile query which will work with all kinds of csv file.Please look into it.
The below query works with all kinds of csv files in which fields separated by ,.
example csv
1,114300,1790,2,2,2,No,East
2,114200,2030,4,2,3,No,East
3,114800,1740,3,2,1,No,East
4,94700,1980,3,2,3,No,East
Query looks like this:
LOAD DATA LOCAL INFILE '$pathname'
IGNORE INTO TABLE $name
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES"
Another csv is like this
Agfa ePhoto 1280;1997;1024.0;640.0;0.0;38.0;114.0;70.0;40.0;4.0;420.0;95.0;179.0
Agfa ePhoto 1680;1998;1280.0;640.0;1.0;38.0;114.0;50.0;0.0;4.0;420.0;158.0;179.0
Agfa ePhoto CL18;2000;640.0;0.0;0.0;45.0;45.0;0.0;0.0;2.0;0.0;0.0;179.0
Agfa ePhoto CL30;1999;1152.0;640.0;0.0;35.0;35.0;0.0;0.0;4.0;0.0;0.0;269.0
Agfa ePhoto CL30 Clik!;1999;1152.0;640.0;0.0;43.0;43.0;50.0;0.0;40.0;300.0;128.0;1299.0
In the query i just changed Fields terminated by ',' to ';'.It works for me.I need everything to be done by in a single query.I am looking for help.
I extracted the csv filename and its path and stored it in a variable named $filename.
$file = new SplFileObject($filename);//it holds the name and location means path to csv
$file->seek(0);//It holds the first line of csv
if (strpos($file, ';') == true){
$field = "FIELDS TERMINATED BY ';'";
}
if (strpos($file, ',') == true){
$field = "FIELDS TERMINATED BY ','";
}
Then edit the query like this
LOAD DATA LOCAL INFILE '$pathname'
IGNORE INTO TABLE $name
$field
OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES"
It works for all the csv files which fields end by , and ;.

Upload csv file to select columns in table

I have CSV file which has 3 columns, and my table has 4 columns(one of it is a id autoincrement which I want to be autoincremented). I want to load the 3 columns from csv to the selected 3 columns in the table.
Tried code:
LOAD DATA LOCAL INFILE 'info.csv' INTO TABLE tbl_countryip (ipstart, ipend, countrycode) FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' ;
which throws error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'' at line 1
The list of columns is in the wrong place in your statement. Follow the syntax in the documentation.
You want something like this:
LOAD DATA LOCAL INFILE 'info.csv'
INTO TABLE tbl_countryip
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(ipstart, ipend, countrycode)
Reference: http://dev.mysql.com/doc/refman/5.5/en/load-data.html
Follow below steps-
1) connect with any gui like sqlyog.
2) select db > select table > right click on table > choose import > import csv data using load...
3) select all columns except primary key.
4) put 1 in ingnore line option if your csv file contains header.
5) click on import
If you want to do by command then command should be as per below-
LOAD DATA LOCAL INFILE 'info.csv' INTO TABLE tbl_countryip FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (ipstart, ipend, countrycode);
Note: If your corrent dir not where csv file exist then you need to give full path.
If your csv file contains header line then you need to exclude it as per below-
LOAD DATA LOCAL INFILE 'info.csv' INTO TABLE tbl_countryip FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (ipstart, ipend, countrycode);
I removed the id column from the table, so both table and csv file had 3 columns, and then ran the load csv command, and then inserted a id column into the table with autoincrement.

MySql error using OPTIONALLY ENCLOSED BY

I am uploading huge csv file in a mysql database using LOAD DATA local INFILE command.
The table has got single field(hids) only which is integer,primary and autoincrement.The query is
$insert_query = "LOAD DATA local INFILE '".$target_path."' INTO table master_huts(hids) OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES ";
The csv file contains,
ID
"343"
"454"
"777"
If I remove the double quotes from csv file then it works fine. How can I tell MySql to ignore double quotes from csv file.
Try this:
$insert_query = "LOAD DATA local INFILE '".$target_path."' INTO table master_huts(hids) FIELDS ENCLOSED BY '”' LINES TERMINATED BY '\n' IGNORE 1 LINES;

Imported CSV is missing fields

I have to import a CSV file to MySQL from Yii. This is my query:
$sql = "LOAD DATA LOCAL INFILE '".addslashes($dir)."'
INTO TABLE `card`
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\n'
IGNORE 1 LINES
(`ID_CARD`,`SERIAL_NO`,`SERIAL_NO_CARD`,`ICCID`,`MSISDN`,`SITE_NAME`,`STATUS`,`DATE_HISTORY`)";
Yii::app()->db->createCommand($sql)->execute();
However, only column ID_CARD was inserted.
What am I doing wrong?
I already tested this on PHPMyAdmin but it did the same thing.
try this,
LOAD DATA LOCAL INFILE 'test.csv'
INTO TABLE card FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (ID_CARD,SERIAL_NO,SERIAL_NO_CARD,ICCID,MSISDN,SITE_NAME,STATUS,DATE_HISTORY)

Categories