Jump to content

Importing CSV into MYSQL through php not working right


Raz3rt

Recommended Posts

Hello,

 

I am trying to import a CSV file into an existing mysql table but it doesn't seem to work well.

 

Here is how my mysql is looking like:

1    a_id               int(11)        AUTO_INCREMENT
2    a_lobecoid         int(7)    
3    a_code             varchar(30)    utf8_general_ci
4    a_omschint         varchar(60)    utf8_general_ci
5    a_beveiligingniv   int(5)
6    a_type             varchar(5)     utf8_general_ci    
7    a_assortiment      int(5)
8    a_discipline       varchar(30)    utf8_general_ci
9    a_brutoprijs       varchar(50)    utf8_general_ci    
10    a_status          varchar(5)     utf8_general_ci    
11    a_levcode         varchar(10)    utf8_general_ci
12    a_omschr_nl       varchar(60)    utf8_general_ci
13    a_omschr_fr       varchar(60)    utf8_general_ci

these are some lines from my CSV file

16158|-H|Factory installed heater|10|S|400|CCTV|45.0|E|  1829|Factory installed heater|Factory installed heater
16159|-IR|Factory installed IR LED ring|10|S|400|CCTV|50.0|E|  1829|Factory installed IR LED ring|Factory installed IR LED ring
9001|00-SBN2|Smoke box niet geaspireerd,230VAC|10|S|267|BRAND|1587.03|D|   642|Smoke box niet geaspireerd,230VAC|Smoke box pas aspiré,230VCA
9003|00-TP1|Telescopische verlengstok voor Smoke box,2,4-4,6m|10|S|267|BRAND|644.09000000000003|D|   642|Telescopische verlengstok voor Smoke box,2,4-4,6m|Rallonge téléscopique pour Smoke box,2,4-4,6m
9004|00-TP2|Telescopische verlengstok voor Smoke box,2,4-9,2m|10|S|267|BRAND|944.64999999999998|D|   642|Telescopische verlengstok voor Smoke box,2,4-9,2m|Rallonge téléscopique pour Smoke box,2,4-9,2m
12161|001-0081|Thermistor probe,rood, high temp. 0-+150°C|10|S|52|INBRAAK|136.91|D|  1731|Thermistor probe,rood, high temp. 0-+150°C|Sonde température,rouge,high temp. 0-+150°C

Here you have the code for the import:

$upload_article_query = "LOAD DATA INFILE 'ARTIKELS.CSV'
                             INTO TABLE artikelen
                             FIELDS TERMINATED BY '|'
                             LINES TERMINATED BY '\\r\\n'
                             (a_lobecoid, a_code, a_omschint, a_beveiligingniv, a_type, a_assortiment, a_discipline, a_brutoprijs, a_status, a_levcode, a_omschr_nl, a_omschr_fr)";
$upload_article_stmt = $dbh->prepare($upload_article_query);
    
    
$upload_article_stmt->execute();

If i use the code then in the MYSQL table the first line is filled in but where the second line has to start it just writes it into the last column and doesn't start a new line.

Also if i edit that first line it shows a lot of "?" (questionmarks) into a window symbol.

 

Anyone has an idea what i am doing wrong?

 

In attachment some printscreens of my table after the insert. Apologies for the Dutch language.

 

Thanks in advance

post-168958-0-12352100-1401133287_thumb.jpg

post-168958-0-81786600-1401133384_thumb.jpg

Link to comment
Share on other sites

This looks like an issue with the CSV content and/or problems with the character encoding.

 

Check the CSV with a hex editor to see what it actually contains; there may be invisible characters. Also make sure that the encodings are correct. Is the CSV file indeed stored as UTF-8? Is character_set_database also UTF-8? If not, use the CHARACTER SET clause

Link to comment
Share on other sites

Well thank you for the answer!

 

@ginerjm: I thought that allready but i think it cannot do any harm to do it that way? It's just how do you call it a habit to write it this way

 

@Jacques1: Yes indeed the CSV is actually in UCS-2 format so there is my problem! I changed it using Notepad++ now i got a whole other issue but one that i can solve i think just by looking at it.

(Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '00-SBN2' for key 'a_code'')

 

Thanks for the help i glad i've learned something!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.