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

如何存储上传的.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
 }

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

我有一个带有以下数据的文件“exmple.dat”。

  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  
   
 
 

我想将这些数据存储到mysql表“scheduler”中。 我使用file_get_contents()读取内容,但是如何将这些内容插入到调度程序表中,其中列为sid,date,time和stat?

  if(!empty($ _ FILES)  )){
 $ file = file_get_contents($ _ FILES [“upload_cont_dat”] [“tmp_name”],true); 
 //将内容存储在表
} 
   
 <  / DIV>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.
    } 
    
    点赞 评论

相关推荐 更多相似问题