Reading first row from a csv file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codeseeker1
    New Member
    • Jan 2009
    • 14

    #1

    Reading first row from a csv file

    hi all,
    i am currently working with a csv file to import data into mysql using php.
    i am able to read all data using the piece of code.
    Code:
    $fd = fopen ("upload/consumption.csv", "r");
    while (!feof ($fd))
    { 	$buffer = fgetcsv($fd, 5000);
    }
    fclose ($fd);
    but i want to read only first row of file.can anyone help to get it?

    thanks in advance
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    fgets() reads one line from a file into an array.
    But if it is a csv file why not use the fgetcsv which parses
    for csv fields? That is you can specify the delimiter

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Just count how many times the while() loop has iterated?

      Code:
      $fd = fopen ("upload/consumption.csv", "r");
      
      $counter = 0;
      
      while ( ! feof ( $fd ) )
      {     
          if ( $counter === 1 )
              break;
          
          $buffer = fgetcsv ( $fd, 5000 ); 
          ++$counter;
      }
      
      fclose ($fd);

      Comment

      Working...