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.
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?