-1

<?php
include_once "delete.php";
?>

<?php
$databasehost = "localhost";
$databasename = "";
$databasetable = "Main_CSV_Table";
$databaseusername="contalso_OK";
$databasepassword = "";
$fieldseparator = ";";
$lineseparator = "\n";
$csvfile = "arquivo1/arquivo1.csv";




if(!file_exists($csvfile)) {
    die("File not found. Make sure you specified the correct path.");
}

try {
    $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", 
        $databaseusername, $databasepassword,
        array(
            PDO::MYSQL_ATTR_LOCAL_INFILE => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );
} catch (PDOException $e) {
    die("database connection failed: ".$e->getMessage());
}

$affectedRows = $pdo->exec("
    LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
      FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
      LINES TERMINATED BY ".$pdo->quote($lineseparator));

echo "Loaded a total of $affectedRows records from this csv file.\n";

?>

I need to remove the header in this php code. Do I need to remove the first line from the csv file?enter code here Alguma Does anyone have a suggestion remove the header

enter image description here

2 Answers2

0

See: https://dev.mysql.com/doc/refman/8.0/en/load-data.html

$affectedRows = $pdo->exec("
    LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
      FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
      LINES TERMINATED BY ".$pdo->quote($lineseparator)."
      IGNORE 1 LINES");
Sammitch
  • 30,782
  • 7
  • 50
  • 77
-1

For remove first line you can adapt this with you code:

//Read File
$file = fopen('filewithfullpath.csv', 'r');
$data=array();
while (($data_tmp = fgetcsv($file, 1000, ",")) !== FALSE) {
$data[] = $data_tmp;
}
fclose($file);

//Remove first line
array_shift($data);
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
  • There are how you adapt in the code I picked up on the internet that works. But we only have to remove the first line. – consultoria soft Dec 22 '19 at 19:52
  • This only applies if you're reading the data with PHP. in this case it's Mysql that is opening and reading the file directly. – Sammitch Dec 23 '19 at 00:42