1

I am getting these errors in the loop in running import from the command line and cant figure out why I am getting this and how to get rid of it

PHP Notice:  Undefined offset: 1 in /home/ubuntu/workspace/pset8/bin/import on line 31

Notice: Undefined offset: 1 in /home/ubuntu/workspace/pset8/bin/import on line 31 PHP Notice: Undefined offset: 2 in /home/ubuntu/workspace/pset8/bin/import on line 32

Notice: Undefined offset: 2 in /home/ubuntu/workspace/pset8/bin/import on line 32 PHP Notice: Undefined offset: 3 in /home/ubuntu/workspace/pset8/bin/import on line 33

Notice: Undefined offset: 3 in /home/ubuntu/workspace/pset8/bin/import on line 33 PHP Notice: Undefined offset: 4 in /home/ubuntu/workspace/pset8/bin/import on line 34

Notice: Undefined offset: 4 in /home/ubuntu/workspace/pset8/bin/import on line 34 PHP Notice: Undefined offset: 5 in /home/ubuntu/workspace/pset8/bin/import on line 35

Notice: Undefined offset: 5 in /home/ubuntu/workspace/pset8/bin/import on line 35 PHP Notice: Undefined offset: 6 in /home/ubuntu/workspace/pset8/bin/import on line 36

Notice: Undefined offset: 6 in /home/ubuntu/workspace/pset8/bin/import on line 36 PHP Notice: Undefined offset: 7 in /home/ubuntu/workspace/pset8/bin/import on line 37

Notice: Undefined offset: 7 in /home/ubuntu/workspace/pset8/bin/import on line 37 PHP Notice: Undefined offset: 8 in /home/ubuntu/workspace/pset8/bin/import on line 38

Notice: Undefined offset: 8 in /home/ubuntu/workspace/pset8/bin/import on line 38 PHP Notice: Undefined offset: 9 in /home/ubuntu/workspace/pset8/bin/import on line 39

Notice: Undefined offset: 9 in /home/ubuntu/workspace/pset8/bin/import on line 39 PHP Notice: Undefined offset: 10 in /home/ubuntu/workspace/pset8/bin/import on line 40

Notice: Undefined offset: 10 in /home/ubuntu/workspace/pset8/bin/import on line 40 PHP Notice: Undefined offset: 11 in /home/ubuntu/workspace/pset8/bin/import on line 41

Notice: Undefined offset: 11 in /home/ubuntu/workspace/pset8/bin/import on line 41

The code for my import file is

<?php

// TODO
// including the file thats has the query function
require("../includes/config.php");

//ensuring proper usage
if($argc != 2)
{
    print("Usage : ./import path/to/file \n");
    exit(1);
}

// if the file is there and can be read
if(is_readable($argv[1]))
{
    $file = $argv[1];
    //open file
    $handle = fopen($file, "r");
    if ($handle == false)
    {
        print("Could not open file./n");
        exit(1);
    }

    // iterate over all the rows
    while($row = fgetcsv($handle, "\t"))
    {
        $countrycode = $row[0];
        $postalcode = $row[1];
        $placename = $row[2];
        $adminname1 = $row[3];
        $admincode1 = $row[4];
        $adminname2 = $row[5];
        $admincode2 = $row[6];
        $adminname3 = $row[7];
        $admincode3 = $row[8];
        $latitude = $row[9];
        $longitude = $row[10];
        $accuracy = $row[11];

        //query to insert into db
        CS50::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 (?,?,?,?,?,?,?,?,?,?,?,?)",$countrycode, $postalcode, $placename,
                                         $adminname1, $admincode1, $adminname2, $admincode2, $adminname3,
                                         $admincode3, $latitude, $longitude, $accuracy);

    }
}
else
{
    print("could not open/read file");
}

fclose($handle);

?>

2 Answers 2

1

Try this fgetcsv($handle, 0, "\t") instead of this fgetcsv($handle, "\t")

Why?

Because the second parameter of fgetcsv() is the length of the line. Thus, if you don't explicitly put it there, fgetcsv() will most likely think that your third parameter (delimiter "\t") is in fact the second.

0

You can kinda see what's wrong if you try to: print_r($row);

You should then see that $row is one array and there is no $row[1]

I haven't solved it yet myself but here is how you split it up:

$data = (list($country_code, $postal_code, $place_name, $admin_name1, $admin_code1, $admin_name2, $admin_code2, $admin_name3, $admin_code3, $latitude, $longitude, $accuracy) = fgetcsv($file, 0, "\t"));

It should then be like this: Array ( [0] => US [1] => 34034 [2] => APO [3] => [4] => AA [5] => Dillon [6] => 033 [7] => [8] => [9] => 33.0364 [10] => -82.2493 [11] => 1 )

Hope this was helpful. Sorry if it is poorly explained.

You must log in to answer this question.

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