doulu4976 2014-03-18 05:35
浏览 30
已采纳

.csv文件进入mysql

The server I'm using does not allow me to use LOAD DATA INFILE or even save a file as a .txt file. I have a .csv file that is just one column, with a name in each row. I want to insert this data into the name column of a table named people with name as one column and location as the other… where the locations will be updated at a later time.

It's a weird way to do this. But I need to do it this way and not using those previous commands.

Does anybody have any ideas? This has been giving me a problem for many hours. I can't figure out how to load the csv into my table column without using those previously mentioned methods that I can't use on my server.

  • 写回答

1条回答 默认 最新

  • doucigua0278 2014-03-18 06:06
    关注

    Based on your issue and lack of general permissions you will have to do the following:

    Replace the DOS carriage returns with unix new lines:

    $contents=preg_replace('/(
    ||
    )/s',"
    ",$contents); 
    

    Save the contents to the file, and then loop through each line, building an INSERT command that you execute to MySQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?