I'm trying to import a .txt file from a local folder where the delimiter is "£" to a mySQL table but it keeps sending it all to the first field.
I'm using this code:
$sql = ("LOAD DATA LOCAL INFILE 'C:/xampp/htdocs/test/test.txt'
INTO TABLE tabletxt
FIELDS TERMINATED BY '£'
LINES TERMINATED BY '\n'
IGNORE 1 LINES (name, country, date);");
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Also I need to convert the date field to date, here is one line of example from the file Luke£France£29.01.2016
Thank you
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.
So, I wanna to get file path and then insert that into mysql.
This is the code:
<?php
$file = $_FILES['file']['tmp_name'];
include "config.php";
$result = mysql_query("LOAD DATA LOCAL INFILE '$file'" .
" INTO TABLE ponuda FIELDS TERMINATED BY ' '");
if (!$result) {
die("Could not load. " . mysql_error());
}
?>
This is the path I receive:
string 'C:\Users\xxx\AppData\Local\Temp\php11EF.tmp'
But, I got mysql error saying this:
Could not load. Can't find file 'C:UsersxxxAppDataLocalTempphp1FA6.tmp'.
Why are slashes removed? What I doing wrong. Tried to search for a problem but didn't find any results.
p.s. I want to upload that file from my local PC.
EDIT: if I change "\" to this "/" in path it works well, but how can I use original file path, not temp one because that file doesn't exist in temp?
OK, managed to find error.
Now it's working:
<?php
$file = str_replace("\\", "/", $_FILES['file']['tmp_name']);
include "config.php";
$result = mysql_query("LOAD DATA LOCAL INFILE '$file'" .
" INTO TABLE ponuda FIELDS TERMINATED BY ' '");
if (!$result) {
die("Could not load. " . mysql_error());
}
?>
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);
?>
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