Search code examples
mysqlpdoload-data-infile

Get number of affected rows in MySQL


trying to get the affected row return 0 file contain 250k records:

$affectedRows = 0;
$affectedRows = $pdo->exec(
        "DELETE FROM tablename WHERE Col3 BETWEEN '2018-01-01 00:00:00' AND '2018-01-31 00:00:00';
        ALTER TABLE tablename AUTO_INCREMENT = 1;
        LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)");

echo var_dump($affectedRows);

Store the CSV but Return 0


Solution

  • Solved with:

    This script is full suported:

    LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
    FIELDS TERMINATED BY ',' 
    LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`)
    

    But i merge it with other in some Query Like that:

    DELETE FROM tablename WHERE date BETWEEN '$Date1' AND '$Date2';
    ALTER TABLE tbalename AUTO_INCREMENT = 1;
    LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`);
    

    this is wrong, becouse $PDO->exec() Cant return the number of affected table or rows.

    Instead of it, to solve i need past an array, becouse my script support it like that:

    $stmtpre[1] = "DELETE FROM tablename WHERE date BETWEEN '$Date1' AND '$Date2';";
    $stmtpre[2] = "ALTER TABLE tbalename AUTO_INCREMENT = 1;";
    $stmtpre[3] = "LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE tablename 
            FIELDS TERMINATED BY ',' 
            LINES TERMINATED BY ' ' ( `Col1`, `Col2`, `Col3`);";
    

    Same this: https://stackoverflow.com/a/52370701/9632001