0

I've been trying to implement import and I've modified the file US.txt by replacing "\t" with " , " on each line just for a better practicality with my code. However, I receive the following error:

enter image description here

Below is my code:

<?php
require("../includes/config.php");

$argument = $argv[1];
$file = (__DIR__ . "/$argument");




// I'm using only is_readable because this function also checks if the file exists on it's own
if(is_readable($file))
{
    echo "File $file found!\n";


    if (($handle = fopen($file, "r")) !== FALSE)
     {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
        {


            $insert = query("INSERT INTO places (country_code, postal_code, place_name, admin_name1, admin_code1, admin_name2, admin_code2, admin_name3, admin_code3, latitude, longitude, accuracy) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], $data[10], 6);


        }
    fclose($handle);
}



}
else
{
    echo "File $file not found or is unreadable!\n";
}
?>

This is how I've modified my US.txt file:

enter image description here

1

The problem is your input data. Your query is expecting 12 values on every line. The first line in your example has 9. You "lost" the blank columns from the text file, presumably when you converted tab to comma. Your input file should look like this:

enter image description here

I 💜 data

| improve this answer | |
  • Thank you, I've re-downloaded the original .txt file and modified my code with "\t" as a delimiter. My table looks like this gyazo.com/45a79891ea3ac96077e3385a833c1d8e . Is it normal ? EDIT: Just checked the readme and those two columns just indicate the community, so I guess not all of them have this value. Thank you a lot for your help ! – Gabriel Dec 18 '15 at 16:45
  • At a glance it looks ok. FWIW mine has accuracy = 0 for the same rows. I don't recall that accuracy column was a factor in the pset, just commenting on a difference. – DinoCoderSaurus Dec 18 '15 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .