dtfbj24048 2015-04-28 15:37
浏览 15
已采纳

如何将数字列表从csv上传到空表中的一列中

I do Hope someone can help, I have a table with 7 columns ( All Empty )

id | unqid | Numbers | Date/Time | IP | UserAgent | Confirmed

Basically what im trying to do in php is browse for my CSV File with a single column of numbers, Then upload and isert the numbers from the CSV into The Numbers Column in mysql Table.

I have done this with php by simply submitting 1 number.

mysql_query("INSERT test_mysql SET number='$number', uid='$uid'") or die(mysql_error()); 

but i have serached high and low and tried many examples and tried myself but im getting nowhere.

  • 写回答

1条回答 默认 最新

  • dongxiezhi0590 2015-04-28 17:04
    关注

    There will be proper libraries to do this but given a simple csv file

    a,b,c
    1,2,3
    4,5,6
    

    then you can loop through it as demonstrated in the docs http://php.net/manual/en/function.fgets.php and get each line.

    Then you can grab the value you want something like this.

    $handle = @fopen("test.csv", "r");
    if ($handle) {
        while (($buffer = fgets($handle, 4096)) !== false) {
            /* this gives you the value */
            echo explode(',',$buffer)[0];
        }
       fclose($handle);
    }
    

    Which gives you the data you want

    a
    1
    4
    

    You would need to improve this to deal with real messy data and skip headers etc before you then inserted each number.

    Another approach would be loading the csv directly into the database table https://dev.mysql.com/doc/refman/5.1/en/load-data.html

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

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算