使用php将txt文件导入mysql表

时间:2014-01-16 08:48:31

标签: php text import

我有包含数据的文本文件。文本文件中的每一行包含12个按空格分隔的数据。我想将相同的数据导入mysql表,每个数据都会转到相应的字段。数据应附加到表中。我知道如何使用html表单附加数据但是,不知道文件操作。有什么建议吗?

5 个答案:

答案 0 :(得分:2)

试试这个

$f = fopen("your_text_file.txt", "r");
while(!feof($f)) { 
  $data = explode(" ", fgets($f));

   $date = $data[0];
   $avgprd = $data[1];
   $level1 = $data[2];
   $level2 = $data[3];
   $level3 = $data[4];
   $level4 = $data[5];
   $level5 = $data[6];
   $level6 = $data[7];
   $level7 = $data[8];
   $level8 = $data[9];
   $level9 = $data[10];
   $level10 = $data[11];
   $level11 = $data[12];

      mysql_query("INSERT INTO `your_table_name` (date, avgprd, level1, level2, level3, level4, level5, level6, level7, level8, level9, level10, level11) 
VALUES ('$date', '$avgprd', '$level1', '$level2', '$level3', '$level4', '$level5', '$level6', '$level7', '$level8', '$level9', '$level10', '$level11')") or die(mysql_error());
    }
    fclose($f);

答案 1 :(得分:1)

position:absolute; top: 0px; left: 0px;

答案 2 :(得分:0)

查看Mysql中的LOAD DATA INFILE。 http://dev.mysql.com/doc/refman/5.1/en/load-data.html

答案 3 :(得分:0)

以Mysql或php代码中的LOAD DATA INFILE为例:

$f = fopen("yourfile.txt", "r");
while(!feof($f)) { 
  $data = explode(" ", fgets($f));
  mysql_query("INSERT INTO some_table (some_data0, some_data1) values (" . $data[0] . ", " . $data[1] . ")");
}
fclose($f);

答案 4 :(得分:-1)

试试这个......

$file = $_FILES['frmUpload']['tmp_name'];           //  Get Temporary filename
if ($file) {
    $handle = fopen($file,"r");                         //  Open the file and read
    while($strBookData = fgetcsv($handle, 10000, " ")) {//To get Array from CSV, " "(delimiter) 
        $strDatas[] = $strBookData;
        $strTableColumn = count($strBookData);              // To Get Column count
    }
}