duansang8388
2014-12-04 13:13
浏览 53
已采纳

将csv导入mysql(代码列表)

So I have a list of codes (100,000) to be exact and I got them delivered in a csv file.

I want to put theses codes in a database so I can later get them using my php script. However my question is how do I get my codes from the file in the right table?

This is what my database looks like at the moment.

http://i.stack.imgur.com/HMxqC.png

The codes from the file need to be inserted in table code. Hope someone can help me out with this one.

图片转代码服务由CSDN问答提供 功能建议

所以我有一个准确的代码列表(100,000),我把它们放在一个csv文件中。< / p>

我想将这些代码放在数据库中,以便以后可以使用我的php脚本来获取它们。 但是我的问题是如何从右表中的文件中获取代码?< / p>

这就是我的数据库目前的样子。

文件中的代码需要插入表格代码中。 有人可以帮我解决这个问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doudilin1225 2014-12-04 13:25
    已采纳

    You probably will find this helpful (pls adjust the table and others names):

    LOAD DATA INFILE 'codes.csv'
    INTO TABLE codes
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '
    '
    IGNORE 1 ROWS; 
    (@id,code,@active)
    SET id = null;
    SET active = '1'
    

    More details: http://www.mysqltutorial.org/import-csv-file-mysql-table/

    点赞 评论
  • duanhan5388 2014-12-04 13:26

    Try this:

    you can upload your csv file and post it. then use this function.

    this is the PHP code:

    function get_csv_file($filename){
    if (($handle = fopen($filename, "r")) !== false) {
    $filesize = filesize($filename);
    $firstRow = true;
    $aData = array();
    while (($data = fgetcsv($handle, $filesize, ";")) !== false) {
        if($firstRow) {
            $aData = $data;
            $firstRow = false;
        } else {
            for($i = 0;$i < count($data); $i++) { //only for csv file
                if($data[$i] != '')
                $aData[] = $data[$i];
            }
        }
    }
    //print_r($aData);
    fclose($handle);
    $finalscrap = array_unique($aData);
         // echo "<pre>";
         // print_r($finalscrap);
         // echo "<pre/>";
    return $finalscrap;   //this is your record as array format
    }
    }
    
    点赞 评论

相关推荐 更多相似问题