Hello guys, i have been working on a new script that will import data from a TXT file to mysql.
Using this code works ok:
$sql = 'LOAD DATA LOCAL INFILE \'logons-16-12-2013.txt\' INTO TABLE pcvsuser COLUMNS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\\r\\n\'';
But if i use this it does not work:
$sql = "LOAD DATA LOCAL INFILE '".$file."' INTO TABLE pcvsuser COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n'";
Now i will show the entire code:
<?php
// Import shared data
include 'config_all.php';
// Connect to Mysql server
$con = mysql_connect("$sqlhost","$sqluser","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Connect to "ad" database
$sel = mysql_select_db("ad", $con);
if (!$sel)
{
die('Could not select DB: ' . mysql_error());
}
//Define a variables for a clean file name
$data = shell_exec ("echo %date%.txt");
$file = "logons-$data";
// Copy file from remote Domain Controler to HTTP Server
$cmdcopy = shell_exec ("copy z:\logons-%date%.txt logons-%date%.txt");
echo $cmdcopy;
// Load data from file to table "pcvsuser" table
$sql = "LOAD DATA LOCAL INFILE '".$file."' INTO TABLE pcvsuser COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n'";
mysql_query($sql) or die ("<br><br>Error: ". mysql_error()." with query ".$sql."");
?>
If i use $sql = 'LOAD DATA LOCAL INFILE \'logons-16-12-2013.txt\' INTO TABLE pcvsuser COLUMNS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\\r\\n\'';
It works but i need to add $file.
The domain controler creates a file per day with specific date, and this script will be runned everyday at 23:55.
Importing as a variable, is esier because i do not have to do it by hand, i do not have to change de date by hand everyday.
What is wrong?