title: Save CSV files into mysql database
body: <p>I have a lot of csv files in a directory. With these files, I have to write a script that put their content in the right fields and tables of my database. I am almost beginner in php language : I have found some bits of code on the Internet. It seems to work, but I am stuck at a stage. Some topics are related on this website, but I did not found the ecat problem.</p>
  
  <p>I have written a php script that permits to get the path and the name of these files. I managed too to create a table whose name depends of each csv (e.g : file ‘data_1.csv’ gives the table data_1.csv in my-sql). All the tables have the three same fields, id, url, value.
  The last thing to do is to populate these tables, by reading each file and put the values separated by ‘|’ character in the right tables. For example, if a line of ‘data_1.csv’ is</p>
  
  <p>8756|htttp://example.com|something written</p>
  
  <p>I would like to get a record in data_1.csv table where 8756 is in id, htttp://example.com in url field, and something written in value field.
  I have found the way to read and print these csv with fcsvget function. But I do not know how to make these lines go into the SQL database. Could anyone help me on this point?</p>
  
  <p>Here is my script below</p>
  
  <pre><code>&lt;?php 
  ini_set('max_execution_time', 300); 
  $dbhost = 'localhost'; 
  $dbuser = 'root'; 
  $dbpass = ''; 
  $conn = mysql_connect($dbhost, $dbuser, $dbpass, true) or die                      ('Error connecting to mysql'); 
  $dbname = 'test_database'; 
  
  mysql_select_db($dbname); 
  
  
  $bdd = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); 
  $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); 
  if (mysqli_connect_errno()) { 
  printf("Connect failed: %s\n", mysqli_connect_error()); 
  exit(); 
  } 
  else { 
  echo "hello &lt;br&gt;"; 
  } 
  
  $dir = 'C:\wamp\www\test';
  $imgs = array();
  
  if ($dh = opendir($dir)) {
  while (($file = readdir($dh)) !== false) {
      if (!is_dir($file) &amp;&amp; preg_match("/\.(csv)$/", $file)) {
          array_push($imgs, $file);
          }
      }
  
      closedir($dh);
  } else {
      die('cannot open ' . $dir);
  }
  foreach ($imgs as $idx=&gt;$img) {
      $class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
      $filePath=$dir . '\\'. $img;
      $file = fopen($filePath, "r") or exit("Unable to open file!"); 
      $nom = 'FILE: ' . $dir . '\\'. $img;
      echo $nom;
      settype($nom, "string");
      echo '&lt;br&gt;';
      $chemin = '&lt;img src="' . $dir . $img . '" alt="' . 
          $img . '"' . $class . ' /&gt;' . "\n";
      echo $chemin;
      $file_name =  basename($filePath);
      $sql = 'CREATE TABLE `' . $file_name . '` (id int(20000), url varchar(15), value TEXT)';
      mysql_query($sql,$conn);
      $handle = fopen($filePath, 'r');
      while (($row = fgetcsv($handle)) !== false) {
      foreach ($row as $field) {
      echo $field . '&lt;br /&gt;';
  }
  }
  fclose($handle);
  
  }
  
  echo ("VERIFY"); 
  for ($i = 1; $i &lt;= 1682; $i++) { 
      echo ("&lt;br&gt;A : " . $i); 
      $checkRequest= "select * from movies where movieID='". $i."'"; 
      echo ("&lt;br&gt;". $checkRequest); 
      if ($result = $mysqli-&gt;query($checkRequest)) { 
      printf("&lt;br&gt; Select ". $i ."returned %d rows.\n", $result-&gt;num_rows); 
      if ($result-&gt;num_rows == 0) { 
  
                      echo ("I : " . $i); 
              } 
  
  
  
  $result-&gt;close(); 
      } 
  } 
  
  $mysqli-&gt;close(); 
  
  ?&gt; 
  </code></pre>