dongwen2794 2018-12-06 10:44
浏览 104
已采纳

如何存储上传的.dat文件中的数据?

I have a file "exmple.dat" with the following data.

    1   2018-09-06 16:29:18 1   
    2   2018-09-06 16:36:03 1   
    3   2018-09-06 16:36:11 1   
    4   2018-09-06 16:36:58 1   
    5   2018-09-06 16:37:56 1   
    6   2018-09-06 16:38:14 1   
    7   2018-09-06 16:43:53 1   

I want to store this data into mysql table "scheduler". I read the contents using file_get_contents() but how do I insert this contents into the scheduler table with columns sid,date,time and stat?

if(!empty($_FILES)) {
 $file = file_get_contents($_FILES["upload_cont_dat"]["tmp_name"], true);
 // store content in table
 }
  • 写回答

1条回答 默认 最新

  • doulu5717 2018-12-06 10:54
    关注

    I made a little script who read the file and parse for each line and get all data. After that I insert it in MySQL database.

    <?php
    $handle = fopen("exmple.dat", "r");
    if ($handle) {
        $con = new mysqli($db_hostname, $db_username, $db_password, $db_databasename);
        while (($line = fgets($handle)) !== false) {
            // process the line read.
            $line_exp = explode(" ", $line);
            $sid = $line_exp[0];
            $date = $line_exp[3];
            $time = $line_exp[4];
            $stat = $line_exp[5];
            $stmt = $con->prepare("INSERT INTO `scheduler` (`sid`, `date`, `time`, `stat`) VALUES (?,?,?,?)");
            $stmt->bind_param("ssss", $sid, $date, $time, $stat);
            $stmt->execute();
        }
        $stmt->close();
        fclose($handle);
    } else {
        // error opening the file.
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波