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

10
  • recommend resolving it on the terminal first. Is there any warnings? Are you using the same database user in the script as the terminal? Commented Nov 3, 2018 at 1: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. Commented Nov 3, 2018 at 1:33
  • I'm pretty certain you can't bind the filename to a parameter as it is not a "data literal". Have you tried LOAD DATA INFILE '$inputfile1'? Commented Nov 3, 2018 at 1:52
  • @Nick correct. The data is processed "too fast". stackoverflow.com/a/2833873/2943403 Commented Nov 3, 2018 at 1:53
  • 1
    @mickmackusa seems 10 years on it is still an outstanding feature request bugs.mysql.com/bug.php?id=39115 Commented Nov 3, 2018 at 1:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.