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";

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

            $str_values = implode(',', $data);

            //echo $str_values . "\n";


            $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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $str_values[0], $str_values[1], $str_values[2], $str_values[3], $str_values[4], $str_values[5], $str_values[6], $str_values[7], $str_values[8], $str_values[9], $str_values[10], $str_values[11], 6);


        }
    fclose($handle);
}



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

The problem is your insert query. You are inserting 12 columns, you have 12 ? placeholders, but you are sending 13 arguments.

Also, beware: implode returns a string. So you $str_values[0] et. al. will the "individual" characters. fgetcsv returns an array, so $data should be the array you want in your insert.

| improve this answer | |
  • Thank you. However, I'm now inserting values as single chars instead of strings for each table row. – Gabriel Dec 18 '15 at 14:45
  • Does that mean you have fixed it and it works? – DinoCoderSaurus Dec 18 '15 at 15:25
  • It does insert in the table, but it inserts just single chars by row instead of the appropriate strings. – Gabriel Dec 18 '15 at 15:26
  • Example shown here : gyazo.com/0a28047ba2a31bae165789a4aa01ddc2 – Gabriel Dec 18 '15 at 15:34
  • Ah. implode returns a string. so $str_values[0] is the first char in the string, $str_values[1] is the second, etc. Since fgetcsv returns an array, you should not need to do any transform on it. I will edit that info to the answer. – DinoCoderSaurus Dec 18 '15 at 15:37

You must log in to answer this question.

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