1

Okay so I have a php script to insert data into a mysql table it looks like this:

<?php
$connect = mysql_connect('localhost','my_username','my_pass');
if (!$connect) {die('Could not connect to MySQL: ' . mysql_error());}
$cid =mysql_select_db('geolocation',$connect);
// supply your database name
define('CSV_PATH','/var/www/geolocation/');
// path where your CSV file is located
$csv_file = CSV_PATH . "new_Location.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
$k = 0;
while (!feof($csvfile)) {
   $csv_data[] = fgets($csvfile, 1024);
   $csv_array = explode(",", $csv_data[$i]);
   $insert_csv = array();
   if(isset($csv_array[0]) && isset($csv_array[1]) &&  isset($csv_array[2]) &&  isset($csv_array[3]) &&  isset($csv_array[4]) &&  isset($csv_array[5]) &&  isset($csv_array[6]) &&  isset($csv_array[7]) &&  isset($csv_array[8])){
     $insert_csv['ID'] = $csv_array[0];
     $insert_csv['country'] = $csv_array[1];
     $insert_csv['region'] = $csv_array[2];
     $insert_csv['city'] = $csv_array[3];
     $insert_csv['postalCode'] = $csv_array[4];
     $insert_csv['latitude'] = $csv_array[5];
     $insert_csv['longitude'] = $csv_array[6];
     $insert_csv['other'] = $csv_array[7];
     $insert_csv['another'] = $csv_array[8];
     $query = "INSERT INTO City (locId, country, region, city, postalCode, latitude, longitude) VALUES('".$insert_csv['ID']."','".$insert_csv['country']."','".$insert_csv['region']."','".          $insert_csv['city']."','".$insert_csv['postalCode']."','".$insert_csv['latitude'].",,'".$in sert_csv['longitude']."'')";
  $n=mysql_query($query, $connect );
   $i++;
   }
   if($k==1000){
    echo $i . " \n";
    $k = 0;
   }
   $k++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysql_close($connect);
?>

now when I run the script everything seems to work properly and I get echo $i every once in a while and eventually the script ends with no errors or anything however when I look into my mysql (through phpmyadmin) I do not see any of the rows added to the table... I have also used another modified version of this script in the past and it worked perfectly (other than slow), However I'm still pretty green when it comes to mysql and can't seem to figure out what's going on... here is my table format

 locId --> int(11)
 country --> varchar(2)
 region --> varchar(2)
 city --> varchar(50)
 postalCode --> varchar(8)
 latitude --> varchar(10)
 longitude --> varchar(10)

and also a sample from the csv file:

 46,CK,NA,NA,NA,-21.2333,-159.7667,NA,NA

any help with why this isn't workign or even how I can go about debugging it would be very much appreciated!

also note (I do not wish to have the last to value in the csv in my table) so those are ommited from the insert statement on purpose

0

1 Answer 1

2

i do something like this:

$file = fopen("$link_file","r")or die("file dont exist");
while (!feof($file )){
$campo = fgetcsv($file,4096,",");

$loadsql = "INSERT INTO temporal_table(id,state,etc) VALUES ('$campo[0]','$campo[1]','etc');";
mysql_query($loadsql) or die(mysql_error());

}
fclose($file );

i dont see you using fgetcsv function. and, did you try to echo the data in the while? use something like this:

echo $i." line query ".$query." <br>";

EDIT

try this:

$csv_file = CSV_PATH . "new_Location.csv"; 
$file = fopen("$csv_file","r")or die("file error");
while (!feof($file)){

$insert_csv = fgetcsv($file,4096,",");
$query = "INSERT INTO City (
               locId, 
               country, 
               region, 
               city, 
               postalCode, 
               latitude, 
               longitude                //removed all the simple quotes..
           ) VALUES (
               '$insert_csv[0]',
               '$insert_csv[1]',
               '$insert_csv[2]',
               '$insert_csv[3]',
               '$insert_csv[4]',
               '$insert_csv[5]',
               '$insert_csv[6]'
           )";
    mysql_query($query) or die(mysql_error());
    }
    fclose($csvfile);
Sign up to request clarification or add additional context in comments.

5 Comments

Your awesome, thanks alot it works :) ... data is slowly trickling in, Much appreciated!
hey... so reached a snag ~12k rows in got this error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'j9p1p8', '48.1167', '-77.7667' )' at line 13root ... any idea why that could be? line 13 is the start of the while loop
mmmm, idk, try to echo the query, echo " line query ".$query." <br>";, and try to execute it in the db, do you have phpmyadmin? .. or paste it here
maybe the problem are the single quote, change all of them, ill edit the post again.
Nope the problem was an apostophe on the line ... I stupidly assumed all the data was safe to insert, however it's not, so I'm fixing this now thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.