title: Import txt file/data to my mysql database
body: <p>I have a txt file with 1000 user names in it. They are seperated by lines. (break line).</p>
  
  <p>I wanna import those user names into my mysql table. I've tried the csv method but it's not as simple as uploading a txt file. Because I need to convert the txt file to a csv file and it's a waste of time.</p>
  
  <p><strong>CSV import code:</strong></p>
  
  <pre><code>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 =&gt; $company) {  
  $insert_string .= "('".$company."')";  
  $counter++;  
  if($counter &lt; $maxsize){  
  $insert_string .= ",";  
  }//if  
  }//foreach  
  mysql_query($insert_string) or die(mysql_error()); 
  </code></pre>
  
  <p>I also tried one method that import a txt data BUT the txt file has to be on the server. It also wastes my time, because if we're talking about big files. It's a waste of time &amp; usage to upload those files to the server and only then start to upload them to the database.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>&lt;?php   
  
  //connect to the database  
  $connect = mysql_connect("localhost","username","password");  
  mysql_select_db("mydatabase",$connect); //select the table  
  //  
  
  if ($_FILES[csv][size] &gt; 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;  
  
  }  
  
  ?&gt;  
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
  &lt;head&gt;  
  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;  
  &lt;title&gt;Import a CSV File with PHP &amp; MySQL&lt;/title&gt;  
  &lt;/head&gt;  
  
  &lt;body&gt;  
  
  &lt;?php if (!empty($_GET[success])) { echo "&lt;b&gt;Your file has been imported.&lt;/b&gt;&lt;br&gt;&lt;br&gt;"; } //generic success notice ?&gt;  
  
  &lt;form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"&gt;  
    Choose your file: &lt;br /&gt;  
    &lt;input name="csv" type="file" id="csv" /&gt;  
    &lt;input type="submit" name="Submit" value="Submit" /&gt;  
  &lt;/form&gt;  
  
  &lt;/body&gt;  
  &lt;/html&gt;
  </code></pre>
  
  <p>I'm interested in the best way to import the txt data, maybe using a text box and I'll paste into it the user names and it will upload them or maybe uploading the txt file but it won't keep it on the server, it will just import what's inside of it.</p>
  
  <p>Looking for help, thanks.</p>