31

I have a CSV file and I read data from CSV file then I want to skip first line of CSV file.Which'll contain any header. I am using this code.

while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
    // Code to insert into database
}

When I insert data into th database then header should not to be saved into the database.

Dharman
  • 30,962
  • 25
  • 85
  • 135
please delete me
  • 311
  • 2
  • 4
  • 8
  • 6
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Jan 17 '13 at 05:36

6 Answers6

79

Before beginning the while loop, just get the first line and do nothing with it. This way the logic to test if it's the first line is not needed.

fgetcsv($file, 10000, ",");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
  //....
}
Dustin Currie
  • 901
  • 6
  • 4
  • 3
    This is the kind of answer that make you think: "why I didn't think in this solution before???". Simple and effective. And if you want to skip x lines, just add a for clause and that's it! – Ricardo Gonçalves Apr 23 '15 at 20:56
  • 1
    I would say this is a better answer than the flag-based solutions, no need to add logic to the inside of your while loop – alex9311 Feb 16 '16 at 13:52
  • 3
    This answer should have been chosen as the best. The simplest solution is always the best. Plus, this is slightly better than the rest in terms of performance – Dany Khalife May 27 '16 at 16:01
48

try:

$flag = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
   if($flag) { $flag = false; continue; }
   // rest of your code
}
Eduard
  • 3,395
  • 8
  • 37
  • 62
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • 1
    I think you are missing a { before the if statement. And a closing } if you wanted to be complete in the example. – 8None1 May 07 '14 at 02:17
11

A bit late, but here is another way to do so (without having to count all the lines): with fgets

$file = fopen($filename, 'r');  // create handler
fgets($file);  // read one line for nothing (skip header)
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
    // do your thing
}

One might consider this to be more elegant

AdrienW
  • 3,092
  • 6
  • 29
  • 59
5

You can add a simple check and skip the query if the check fails:

$firstline = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
    if (!$firstline) {
        // Code to insert into database
    }
    $firstline = false;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
uncovery
  • 794
  • 5
  • 19
4

Try this simple code.

$file = fopen('example.csv', 'r');  // Here example is a CSV name
$row = 1;
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
// $line is an array of the csv elements
if($row == 1){ $row++; continue; }   // continue is used for skip row 1
// print_r($line);
// rest of your code
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Amarendra Kumar
  • 858
  • 9
  • 16
4

You should use the fseek() method in order to get the desired line, regardless the current pointer position.

At this example, get the first line after the loop:

$file = fopen($path, "r");    

while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
  // ...
}

fseek($file, 1, SEEK_CUR);

You can use the third parameter to position the pointer in the file, as is:

SEEK_SET – It moves file pointer position to the beginning of the file.

SEEK_CUR – It moves file pointer position to given location.

SEEK_END – It moves file pointer position to the end of file.

  • This is the best answer! Just get the desired line informing its position. The current accepted answer involves doing checks at every clock of the loop... – Lennon May 29 '19 at 20:54