0

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?

5
Contributors
11
Replies
47
Views
3 Years
Discussion Span
Last Post by Abdul Waseem
Featured Replies
  • $data = shell_exec ("echo %date%.txt"); Make sure above code does not outputs any type of extra chars or EOL chars. Read More

  • 1

    If you `echo $sql;` do you see the correct query? Read More

  • 1
    cereal 1,524   3 Years Ago

    It seems there's an extra space near the end of the filename, run the trim function against the $file variable: `$file = trim($file);` before populating the $sql variable. Read More

  • So simple.... i could cry... `$file = trim($file);` works like a charm thanks very much **cereal** Thanks everybody for the efford Read More

2
$data = shell_exec ("echo %date%.txt"); 

Make sure above code does not outputs any type of extra chars or EOL chars.

0

I already tested it with

$data = shell_exec ("echo %date%.txt");
$file = "logons-$data"
echo $file;

but i need to say that when i am using

$sql = "LOAD DATA LOCAL INFILE '".$file."' INTO TABLE pcvsuser COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n'";

The error that it shows is (Can't find File)

Edited by João_5

0

Error echoed:

Error: Can't find file 'logons-16-12-2013.txt '. with query LOAD DATA LOCAL INFILE 'logons-16-12-2013.txt ' INTO TABLE pcvsuser COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'

if instead of '".$file."' i insert **\'logons-16-12-2013.txt** it loads the file to the db.

Edited by João_5

0

Ok i set the script to show everything with detailed comments:

--->Copy files from domain controler to http server:
ECHO OF VARIABLE $ cmdcopy: 1 file(s) copied. 

--->Shows if variable $ data is defined correctrly:
ECHO OF VARIABLE $ data: 16-12-2013.txt 

--->Shows if variable $ file is defined correctrly:
ECHO OF VARIABLE $ file: logons-16-12-2013.txt 

--->Shows if variable $ sql is defined correctrly:
ECHO OF VARIABLE $ sql: LOAD DATA LOCAL INFILE 'logons-16-12-2013.txt ' INTO TABLE pcvsuser COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'

--->Results of the MySql query:
ERROR: Can't find file 'logons-16-12-2013.txt '. WITH QUERY: LOAD DATA LOCAL INFILE 'logons-16-12-2013.txt ' INTO TABLE pcvsuser COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'

This is the result:

93506b809421d094ec25469b4d0d0793

Edited by João_5

Attachments date.phpresults_.PNG 36 KB
1

It seems there's an extra space near the end of the filename, run the trim function against the $file variable: $file = trim($file); before populating the $sql variable.

0

Instead of trim i user this:

If i user $file shell_exec ("echo logons-%date%.txt");
I get the file name like logons-16-12-2013 .txt
But if i use $data = shell_exec ("echo %date%.txt");
and define $file = "logons-$data";
I get the file name like logons-16-12-2013.txt

I will test with trim

1

So simple.... i could cry...
$file = trim($file); works like a charm

thanks very much cereal

Thanks everybody for the efford

Edited by João_5

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.