将 txt file/data 导入我的 mysql 数据库

Import txt file/data to my mysql database

我有一个包含 1000 个用户名的 txt 文件。它们由行分隔。 (断线)。

我想将这些用户名导入我的 mysql table。我已经尝试过 csv 方法,但它不像上传 txt 文件那么简单。因为我需要将txt文件转换成csv文件,这很浪费时间。

CSV导入码:

mysql_connect('localhost', 'user', 'pass') or die(mysql_error());   
mysql_select_db('db_name') or die(mysql_error());   

$lines = file('import.txt');   
$company_names = "";   
$insert_string = "INSERT INTO `ImportedWordList`(`Word`) VALUES";  
$counter = 0;  
$maxsize = count($lines);  
foreach($lines as $line => $company) {  
$insert_string .= "('".$company."')";  
$counter++;  
if($counter < $maxsize){  
$insert_string .= ",";  
}//if  
}//foreach  
mysql_query($insert_string) or die(mysql_error()); 

我也尝试了一种导入 txt 数据的方法,但 txt 文件必须在服务器上。这也浪费了我的时间,因为如果我们谈论的是大文件。将这些文件上传到服务器然后才开始将它们上传到数据库是浪费时间和使用。

代码:

<?php   

//connect to the database  
$connect = mysql_connect("localhost","username","password");  
mysql_select_db("mydatabase",$connect); //select the table  
//  

if ($_FILES[csv][size] > 0) {  

    //get the csv file  
    $file = $_FILES[csv][tmp_name];  
    $handle = fopen($file,"r");  

    //loop through the csv file and insert into database  
    do {  
        if ($data[0]) {  
            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES  
                (  
                    '".addslashes($data[0])."',  
                    '".addslashes($data[1])."',  
                    '".addslashes($data[2])."'  
                )  
            ");  
        }  
    } while ($data = fgetcsv($handle,1000,",","'"));  
    //  

    //redirect  
    header('Location: import.php?success=1'); die;  

}  

?>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
<title>Import a CSV File with PHP & MySQL</title>  
</head>  

<body>  

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>  

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">  
  Choose your file: <br />  
  <input name="csv" type="file" id="csv" />  
  <input type="submit" name="Submit" value="Submit" />  
</form>  

</body>  
</html>

我对导入 txt 数据的最佳方式很感兴趣,可能是使用文本框,我会将用户名粘贴到其中,然后它会上传它们,或者可能会上传 txt 文件,但不会将其保存在服务器上,它只会导入其中的内容。

寻求帮助,谢谢。

看看这段代码,它应该能满足您的目的:

<?php
$input_file = fopen("your_input_file.txt", "r") or die("Unable to open file!");
$output_file = fopen("your_output_file.txt", "w");
while(!feof($input_file)) {
    $user = fgets($input_file);
    $user = trim($user);
    $query = "insert into table_name values ('" . $user . "');\n";
    fwrite($output_file, $query);
    echo $query;
}
fclose($input_file);
fclose($output_file);
?>

只需将 your_input_file 和 your_output_file 替换为您的输入和输出文件的文件名。

一些附加信息:

  1. 我已经使用 trim 函数截断了将由用户读取的尾随 \n(因为你说你的文件有换行符作为分隔符)
  2. 如果输出文件不存在,将创建输出文件,所以您不必担心。只需输入一个有效的文件名。
  3. 输入文件必须位于此 php 代码所在的目录中。同样,输出将在该目录中创建。