0

I am using the below php script and can't figure out why it is not working. I do get the echo that it processed with no errors in terminal, but the table remains empty. Is there something incorrect with the code causing this. I confirmed it is locating the file in the directory as well. Hopefully something simple is missing.

<?php

try {

$inputfile1 = '////MainDirectory/SubDiretory/data.csv';
$table1 = 'database.testTable1';    
$time1 = microtime(true);

require_once("configFile.php"); //this changes the below as now this is the reference point
    $mysql_host = DB_HOST;
    $mysql_database = DB_NAME;
    $mysql_username = DB_USER;
    $mysql_password = DB_PASS;

    $db = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
    // set the PDO error mode to exception
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

} catch (Exception $e) {
    die("Unable to connect: " . $e->getMessage());
}


try {
    // Return errors
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Begin transaction
    $db->beginTransaction();


if(file_exists($inputfile1)) {
$Found = 'found';
// Query D2 Load Temp table
        $tempA2 = $db->prepare("LOAD DATA INFILE :inputfile1
        INTO TABLE $table1
        FIELDS TERMINATED BY ','
        OPTIONALLY ENCLOSED BY '\"'
        LINES TERMINATED BY '\r\n'
        IGNORE 1 LINES
            (column1, column2, column3);");
        $tempA2->bindParam(':inputfile1', $inputfile1);     
        $tempA2 -> execute();
}


} catch (Exception $e) {
    // If transaction fail, use checkpoint and rollback
    $db->rollBack();
    echo "Conversion failed: " . $e->getMessage().'<br />';
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}   


//time of script    
    //sleep(1);
    $timeC = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
    echo "Full Process Time: {$timeC} $Found";



?>

CSV format:

"HeaderData1","HeaderData2","HeaderData3","HeaderData4","HeaderData5"
"DataA","DataB","DataC"
"Data2","Data2","Data2"

MySQL table format

    id        INT  PK with AI and NN
    column1   Varchar
    column2   Varchar
    column3   Varchar

Thank you for any help

newpie
  • 77
  • 8
  • recommend resolving it on the terminal first. Is there any warnings? Are you using the same database user in the script as the terminal? – danblack Nov 03 '18 at 01:06
  • hello, I am using the same database user, My process is I run the php script on the terminal and it echos that last bit in the script. I don't see any warnings with above script. Does the number of columns have to match as I thought I could have non matching number columns in the past. – newpie Nov 03 '18 at 01:33
  • I'm pretty certain you can't bind the filename to a parameter as it is not a ["data literal"](http://php.net/manual/en/pdo.prepare.php). Have you tried `LOAD DATA INFILE '$inputfile1'`? – Nick Nov 03 '18 at 01:52
  • @Nick correct. The data is processed "too fast". https://stackoverflow.com/a/2833873/2943403 – mickmackusa Nov 03 '18 at 01:53
  • 1
    @mickmackusa seems 10 years on it is still an outstanding feature request https://bugs.mysql.com/bug.php?id=39115 – Nick Nov 03 '18 at 01:58
  • @Nick I did try the literal route, but when I did I get an error in the terminal " 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 '////MainDirectory/SubDiretory/data.csv' at line 1
    Full Process Time: 0.014801979064941 found"
    – newpie Nov 03 '18 at 02:23
  • I just tried your query with the two variables substituted on my local MySQL server (5.7.18) and it works fine. – Nick Nov 03 '18 at 02:30
  • @Nick. Ok glad it worked on yours, now I just need to figure out what is going on with mine. I will try some more variations. Thanks for trying. – newpie Nov 03 '18 at 02:36
  • I wonder what your php and mysql versions are. – mickmackusa Nov 03 '18 at 02:38
  • Something i just discovered. I tested truncating the table, then ran the php script. I refreshed the MySQL in workbench and it was blank. I then added a record to the database and applied it. The Auto-increment was 3 for the new record I added manually. This is telling me that something is getting thru, but it doesn't want to show or populate. Very strange. PHP version 5.6.37, MySQL 14.14 – newpie Nov 03 '18 at 02:55

0 Answers0