I managed to import file from US.txt to places
table and all seems ok, except that only rows from even numbers in file are being imported. Instead of 40975 rows I have 20487.
I tried with while loop(fread, fgetcsv, fgets, feof methods), for loop with 40975 iterations(btw. which isn't working at all). I tried to print the output in console with similar file containing only few rows but output is even weirder. I'm out of ideas to solve this.
#!/usr/bin/env php
<?php
require("../includes/config.php");
$argv;
// open file
$file = fopen($argv[1], "r");
// error handling (http://stackoverflow.com/questions/24753821/php-fopen-error-handling)
if (!file_exists($argv[1]))
{
throw new Exception('File not found.');
}
if (!$file)
{
throw new Exception('File open failed.');
}
// read every line
while(fgetcsv($file, 500, "\t"))
{
$csvarray = [];
$csvarray = fgetcsv($file, 500, "\t"); // enclosure optional 4th argument deleted
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $csvarray[0], $csvarray[1], $csvarray[2], $csvarray[3], $csvarray[4], $csvarray[5], $csvarray[6], $csvarray[7], $csvarray[8], $csvarray[9], $csvarray[10], $csvarray[11]);
}
// close file
fclose($file);
?>