0

I have a large CSV I am writing a PHP CLI script to import into an existing PHP application, while utilizing the existing application's ORM to do relationship management between fields (trust me, I looked at a SQL CSV import, its easier this way). The issue at hand is that the CSV data I have is either malformed, or I have my fgetcsv() call wrong.

This is an example data set i'm aiming to import:

id,fname,lname,email\n
1,John,Public,johnqpublic@mailinator.com\n
1,Jane,Public,janeqpublic@mailinator.com\n

And the CSV Import code pretty much takes from the PHP Docs on fgetcsv():

function import_users($filepath) {
  $row = 0;
  $linesExecuted = 0;

  if(($file = fopen($filepath, 'r')) !== false) {
    $header = fgetcsv($file); //Loads line 1

    while(($data = fgetcsv($file, 0, ",")) !== false) {
      $userObj = user_record_generate_stdclass($data);
      //A future method actually pipes the data through via the ORM
      $row++;
    }
    fclose($file);
  } else {
    echo "It's going horribly wrong";
  }

  echo $row." records imported.";
}

The resulting logic of this method is pretty much a % sign, which is baffling. Am I overlooking something?

Cameron Kilgore
  • 383
  • 7
  • 25
  • simple test: `var_dump($data)` to see what fgetcsv slurped in, then `var_dump($userObj)` to see what your function produced. – Marc B Jan 02 '15 at 20:12
  • Did you try LOAD DATA IN FILE ? – Indra Kumar S Jan 02 '15 at 20:24
  • You don't say what the problem is, aside from "The resulting logic..." How do you know the problem lies in the CSV import? Perhaps it's in `user_record_generate_stdclass()`? And (this is a matter of personal taste) I often find it easier to dispense with all the `fopen()` stuff -- in this case I would use `file()` and `str_getcsv()`. – miken32 Jan 02 '15 at 20:51
  • @miken32 I did some debugging to at least confirm I can get into the iterator for the file. It never returned any `echo`ed string inside that iterator. – Cameron Kilgore Jan 02 '15 at 22:22

0 Answers0