A
A
Angelxalfa2014-12-19 19:31:36
PHP
Angelxalfa, 2014-12-19 19:31:36

How to export data from csv to MySQL?

Good day. There is a csv file with a database dump, it has about 100,000 lines, and 14 columns with data (I only need 7 of them).
At first I tried to export like this:

<?php 
    include '../../connect.php';

   LOAD DATA INFILE 'in.csv' INTO TABLE table FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 ROWS;
?>

However, this code did not work at all (no result). And there are extra "columns" in the source file, and with this method, as far as I understand, you need to create fields in the table according to the number of columns in the csv file and fill in all the fields line by line. Therefore, the idea was to export all the data, and then delete unnecessary fields in the MySQL table.
Then I came across another export method and now I'm exporting data to an Msql table with this code:
$csv_open = fopen("in.csv", "r"); 
   $r = 0; 
while (($row = fgetcsv($csv_open, 1000, ';', '"')) != FALSE) 
   {
        $r++;
      if($r == 1) {continue;} // Не дает записать в БД первую строку 
      $ins="INSERT INTO `table` (`id`, `street`, `house_number`, `app_number`, `description`, `device_id`, `old_meterage`) VALUES ('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[8]', '$row[10]', '$row[13]')";
      mysql_query($ins);
      echo mysql_errno() . ": " . mysql_error(); 
   }
   fclose($csv_open);

Questions:
  1. Everything works fine (the necessary data is saved in the right cells), but about 7000 rows are exported (moreover, with repeated attempts - from 7000 to 8000 rows), and it should be about 100,000. What's the problem?
  2. And another question - the source file in csv comes in windows encoding - 1251. You have to manually convert the encoding to UTF-8 before running the export script. Is there a way to make the script do this automatically?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2014-12-19
@FanatPHP

-one. MySQL Query Rules
0. LOAD DATA INFILE
1. What's the Problem?
2. SET NAMES cp1251

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question