How to remove the top line in a CSV file (coloumn headers)?

I have compiled a script that will upload a CSV file and then extract the data into an already created table. I want to make sure that the first row (column headings) is not inserted into the table, but the rest of the data will be.

  $fp = fopen($_SESSION['filename'],"r");


while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)

{
    $import="INSERT into csv_table(name,address,age) values('$data[0]','$data[1]','$data[2]')";

    mysql_query($import) or die(mysql_error());

}

fclose($fp);

this is part of the code that I use to extract data from a csv file.

Thank you very much for any help in this matter!

+3
source share
3 answers

Just put the following before the loop whileto read the first line:

fgetcsv($fp, 1000, ",");

Then the cycle whilestarts on the second line.

+11
source

Think about it.

, , if.

+1

Just read the space as such:

$fp = fopen($_SESSION['filename'],"r");
$headerLine = true;

while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)

{
        if($headerLine) { $headerLine = false; }
        else {
                $import="INSERT into csv_table(name,address,age) values('$data[0]','$data[1]','$data[2]')";

                mysql_query($import) or die(mysql_error());
        }

}

fclose($fp);
0
source

Source: https://habr.com/ru/post/1703680/


All Articles