title: Import data from a txt file in a MySQL database with PHP on visit
body: <p>I'm using PHPMyAdmin to run my MySQL database.</p>
  
  <p>Suppose we have this txt file "people.txt", a MySQL database and a PHP page in which are showed the data from the database. Suppose that data in the text file are stored with this syntax:</p>
  
  <pre><code>2015/16/01|Alex|Paris  
  2015/13/01|Johnny|Berlin  
  2015/11/01|Mary|Oslo
  </code></pre>
  
  <p>You can notice that each field is separated with a |</p>
  
  <p>Is there any way to import these data using a PHP script? I want to show you a different script that, when the page is visited, send data to the database:</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "my_db";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "INSERT INTO `People` (data, name, city)
  VALUES ('2015/10/01', 'Will', 'Rome')";
  
  if ($conn-&gt;query($sql) === TRUE) {
      $last_id = $conn-&gt;insert_id;
      echo "OK!";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>I want to emulate this script in order to let check, each time the page is visited, the txt file. Any help?</p>
  
  <hr>
  
  <p>I tried to merge the PHP script that shows my data and the one that import them from the txt file but it doesn't seem to work properly..</p>
  
  <pre><code>&lt;?php
  $con=mysqli_connect("localhost","username","","my_db");
  // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed: " . mysqli_connect_error();
  }
  
  $sql = "
      LOAD DATA INFILE 'people.txt'
      INTO TABLE `People`
      FIELDS TERMINATED BY '|'
  ";
  
  $result = mysqli_query($con,"SELECT * FROM `People`");
  
  echo "&lt;table class='people'&gt;
  &lt;tr class='titles'&gt;
  &lt;th&gt;Data&lt;/th&gt;
  &lt;th&gt;Name&lt;/th&gt;
  &lt;th&gt;City&lt;/th&gt;
  &lt;/tr&gt;";
  
  while($row = mysqli_fetch_array($result))
  {
  echo "&lt;tr&gt;";
  echo "&lt;td&gt;" . $row['Data'] . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['Name'] . "&lt;/td&gt;";
  echo "&lt;td&gt;" . $row['City'] . "&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  }
  echo "&lt;/table&gt;";
  
  mysqli_close($con);
  ?&gt;
  </code></pre>