LOAD DATA INFILE from cvs enclosed with double quotes fields - php

So, I got this. Just trying to insert a csv file into a MySQL through PHP PDO driver:
<?php
$databasehost = "localhost";
$databasename = "db";
$databasetable = "table";
$socketPath = "/home/mysql/mysql.sock";
$databaseusername="user";
$databasepassword = "pass";
$fieldseparator = ",";
$fieldenclosed = '"';
$lineseparator = "\r\n";
$csvfile = "file.csv";
if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}
try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename;unix_socket=$socketPath",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator).", ENCLOSED BY ". $pdo->quote($fieldenclosed)."
LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES;");
echo "Loaded a total of $affectedRows records from this csv file.\n";
?>
The csv file is something like this(comma separated and enclosed by double quotes).
"X410","","4114068500","000010","04/15/2014","04/16/2015"
"X410","","4220521243","000030","04/08/2014","04/08/2015"
"X410","","4130003659","000010","04/02/2014","04/05/2014"
"X410","","4220524277","000010","04/08/2014","04/08/2015"
"X410","","4114038136","000010","04/07/2014","04/07/2015"
"X410","","4130003594","000110","03/14/2014","03/14/2015"
"X410","","4130003675","000010","04/04/2014","04/04/2015"
"X410","","4130003623","000010","03/12/2014","03/12/2015"
"X410","","4130003679","000010","04/09/2014","04/09/2015"
"X410","","4130003679","000020","04/09/2014","04/09/2015"
THe ENCLOSED BY part is the one that's giving me trouble, I've done my homework and tried $fieldenclosed = '\"\"', $fieldenclosed = "\"\"", with and withouth the $pdo->quote($fieldenclosed) and any other weird concat stuf I've though of or found out in other topics with similar issues.
MySQL throws this error:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 ' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n' IGNORE 1 LINES' at line 2' in /root/csvUpload.php:32
Im running MySQL 5.1 in a CentOS 6.4 server.
Any ideas?

There shouldn't be a comma before ENCLOSED BY. It should be:
$fieldenclosed = '"';
Just a single quote -- it's the character that's supposed to be at the beginning and end of the field.
You also mistyped the variable name, didn't end the string and concatenate around calling $pdo->quote.
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." ENCLOSED BY ".$pdo->quote($fieldenclosed)."
LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES;");

Related

load data infile function with PHP

I have tried to import CSV file into MySQL database using PHP and MySQL load data Infile function.The query looks correct but still there is an error, while embedding it inside php code. Would you Please help me.
<?php
//connect to the database
$host='localhost';
$username='';
$password='';
$database_name='cricket';
$connection=mysql_connect($host,$username,$password,$database_name);
if(!$connection)
{
echo "no connection made";
}
//
else {
$query="LOAD DATA INFILE 'cricket.csv'
INTO TABLE cricket_batsman
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'";
mysql_query($query);
}
?>

Need to upload stats data from a csv file to sql database automatically every day?

Ok so im trying to use phpjobscheduler to update stats every day, It runs a command every day to open the php page and updates the db.
I have Googled and this is the code I managed so far:
<?php
$mysqli = new mysqli('localhost','xxxxx','xxxxx','xxxxx');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "LOAD DATA LOCAL INFILE 'xxxxx.CSV' REPLACE INTO TABLE `Stats_Day` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#txdate, department, code, descript, QTY, Total) SET txdate = STR_TO_DATE(#txdate, '%Y/%m/%d')";
$res = $mysqli->query($query);
if ($mysqli->error) {
try {
throw new Exception("MySQL error $mysqli->error <br> Query:<br> $query", $msqli->errno);
} catch(Exception $e ) {
echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br >";
echo nl2br($e->getTraceAsString());
}
}
mysqli_close($con);
?>
This however is not working it gives me a blank page,
and nothing is updated on the SQL-database?
it connects to the db
(enter wrong details it shows incorrect user details)
but is not updating the db ? and shows no errors?
Please assist?
all I need to do is to update the db with the data from the .csv file every day at 1am.
You need to escape the double quote at ENCLOSED BY part. Without that, you terminated your string.
$query = "LOAD DATA LOCAL INFILE 'xxxxx.CSV' REPLACE INTO TABLE `Stats_Day` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (#txdate, department, code, descript, QTY, Total) SET txdate = STR_TO_DATE(#txdate, '%Y/%m/%d')";
Note
Use error_reporting(E_ALL), and ini_set('display_errors', 1); to cactch your errors while developing.

mysql load data infile through php script - not working

I am trying to use the mysql LOAD DATA LOCAL INFILE to get some csv data into my mysql database through a php script using mysqli. This is what my sql string looks like:
LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv' INTO TABLE dashmaker.HHdata FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
This is what my php script looks like:
$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
INTO TABLE dashmaker.HHdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","[user]","[password]","[database]");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($sql, $con);
if (mysql_affected_rows() == 1) {
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysql_error();
};
echo $message;
mysqli_close($con);
I found that I needed to set the mysql my.cnf to include local-infile under [mysql] and [mysqld] - so I have done that.
When I run the sql query through the shell it works. When I try to do it through the php script the error message ($message) I now get says:
The user update failed: Access denied for user ''#'localhost' (using password: NO)
One weird thing is that it doesn't show any user name before the #'localhost'. Can't understand why. Besides this, I use the same connection setting to run regular SELECT queries from the database using php scripts. The user also has FILE privileges.
I have searched extensively but haven't found anything that can explain what's going on. Any advice would be much appreciated.
You're mixing MySQL APIs with mysql_ and mysqli_ functions in a few instances.
mysql_affected_rows()
mysql_error()
They do not mix together; use mysqli_ exclusively.
Plus, you're not using brackets in [user] etc, are you? That is MSSQL syntax, remove them.
Plus, in mysqli_, DB connection comes first, invert these $result = mysqli_query($sql, $con); to read as $result = mysqli_query($con, $sql);
$sql = "LOAD DATA LOCAL INFILE '/var/www/html/dashmaker/uploads/HHdata.csv'
INTO TABLE dashmaker.HHdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($con, $sql);
if (mysqli_affected_rows($con) == 1) {
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysqli_error($con);
};
echo $message;
mysqli_close($con);

import glob text file from php to mysql

My glob code finds the file and attempts to insert it into the table but fails. I couldn't find anyone that is asking the same question. I will have many files that need to be inserted into the same table. It is a coma delimited text file.
Updated code*
The following code works as long as I rem the load data line. When I use the load data command manually, it works. I have not found a post example that has solved the problem "Why does the load data command not work?"
$con = new mysqli('localhost', 'username', 'password', 'schaadan_hash');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connected. ";
echo "<br />";
}
$upload = "LOAD DATA LOCAL INFILE 'ntlmhash2_1.txt' INTO TABLE ntlm2 FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\n'(`clear` , `hash`);";
mysqli_query($con,$upload);
?>

Got error message in LOAD DATA LOCAL INFILE

I've got this error:
Error message is :: "Error occured during query execution: (LOAD DATA LOCAL INFILE '/tmp/3sch.jofcial.txtgymCN5' REPLACE INTO TABLE `prod_sch` FIELDS TERMINATED BY ',' IGNORE 1 LINES (No_ ,Model_Code,Model_Name ,Lot_No_ ,Start_Seq_No_ ,Quantity ,Lot_Quantity ,Line_Code ,Line_Name ,#var10, Shift_No1,#var12,Shift_No2,#var14,Production_Mngm_Type,Model_Group,Model_Category,Actual_Results,MPS_Mngm_Code, Production_Mngm_Code,Comment) SET Production_Start_Date=STR_TO_DATE(#var10,'%m/%d/%Y'), Production_Finish_Date=STR_TO_DATE(#var12,'%m/%d/%Y'), Due_date=STR_TO_DATE(#var14,'%m/%d/%Y')): The used command is not allowed with this MySQL version";
After try this syntax:
$temp_file = tempnam(sys_get_temp_dir(), '3sch.jofcial.txt'); // make temporary file name
$fp = fopen($temp_file, "wb"); // open temprary file
if (!$fp) die(_ERROR14);
fwrite($fp, $httpfile); // copy to temporary file from schedule file
fclose($fp);
chmod($temp_file, 0644); // file mode change for read only
$sql="TRUNCATE TABLE `prod_sch`";
$res=mysql_query($sql) ;//or _doError(_ERROR30 . ' (<small>' . htmlspecialchars($sql) . '</small>): ' . mysql_error() ); // submit SQL to MySQL $
$sql="LOAD DATA LOCAL INFILE '".$temp_file."' REPLACE INTO TABLE `prod_sch` FIELDS TERMINATED BY ',' IGNORE 1 LINES "; // FIELDS ENCLOSED BY '\"'$
$sql.="(No_ ,Model_Code,Model_Name ,Lot_No_ ,Start_Seq_No_ ,Quantity ,Lot_Quantity ,Line_Code ,Line_Name ,#var10, ";
$sql.="Shift_No1,#var12,Shift_No2,#var14,Production_Mngm_Type,Model_Group,Model_Category,Actual_Results,MPS_Mngm_Code, ";
$sql.="Production_Mngm_Code,Comment) ";
$sql.="SET Production_Start_Date=STR_TO_DATE(#var10,'%m/%d/%Y'), ";
$sql.="Production_Finish_Date=STR_TO_DATE(#var12,'%m/%d/%Y'), ";
$sql.="Due_date=STR_TO_DATE(#var14,'%m/%d/%Y')";
$res=mysql_query($sql) or _doError(_ERROR30 . ' (<small>' . htmlspecialchars($sql) . '</small>): ' . mysql_error() ); // submit SQL to MySQL and$
unlink($temp_file);
This error happen after restore the backup file into new server. From the error message I can see that the temporary file already created.
EDIT
I have try:
mysql --load-infile -u root -p pwd DBname
then try to put the query above inside >mysql, this query can work.
But, why if i using php it doesn't?
try to edit /etc/mysql/my.cnf:
add local-infile = 1
still can't work.
this the php page
Problem solved
Just change:
$dbc=mysql_connect(_SRV, _ACCID, _PWD) or die(_ERROR15.": ".mysql_error());
Into :
$dbc=mysql_connect(_SRV, _ACCID, _PWD,false,128) or die(_ERROR15.": ".mysql_error());
Tadaaa...all error is gone!!
From here:
If LOAD DATA LOCAL is disabled, either in the server or the client, a client that attempts to issue such a statement receives the following error message:
ERROR 1148: The used command is not allowed with this MySQL version

Categories