$BICostumer=$_POST['CostumerId'];
date_default_timezone_set('America/Phoenix');
$today =date("Y-m-d_H:i:s");
$sql =$bdd->query("SELECT * INTO OUTFILE 'shared/$BICostume+$today.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM ListSaving WHERE biid ='$BICostumer';");
in this part of the query 'shared/$BICostume+$today.csv' i want the file to be outfiled with 2 variables as a name of the file.
each time i export the file i want to get something like biide13_03/26/2014_10:25:11.CSV and next export like
WFGT34_03/26/2014_11:50:11.CSV
Plus sign might be causing issue when used in a file name. Try replacing it with an underscore like:
$sql =$bdd->query("SELECT * INTO OUTFILE 'shared/{$BICostume}_{$today}.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM ListSaving WHERE biid ='$BICostumer';");
Edited variable usage inside query after #Rocket Hazmat's warning
Related
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.
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 ;.
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);
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)
Following is my sql query which worked fine when i ran it from phpmyadmin but when i ran the same query through PHP then the query didnot worked and gave me following error. Kindly let me know how can I run this query from my php file so it will create the output file in the specified folder. Thanks,
QUERY
SELECT * FROM lahore_student INTO OUTFILE 'b://uploaded//data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Output: It creates file to my b drive in FOLDER named as Uploaded
ERROR:
File 'b:uploadeddata.csv' already exists
NOTE: The uploaded folder was empty
It looks you are trying to run this on a windows system, which requires special treatment of directory separators.
Try:
SELECT * FROM lahore_student INTO OUTFILE 'b:\\uploaded\\data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Or:
SELECT * FROM lahore_student INTO OUTFILE 'b:/uploaded/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Also note that this will not overwrite an existing file (which is the error you are getting). So you need to make sure that any file with that name is deleted first.
See the The “\” path name separator character section on this page in the MYSQL docs:
http://dev.mysql.com/doc/refman/5.0/en/limits-windows.html