Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. <?php
  2.  
  3. $host = 'localhost';
  4. $user = 'root';
  5. $pass = '';
  6. $database = 'ingrid';
  7. $file = 'ABBREV.csv';
  8.  
  9. $db = new PDO("mysql:dbname=".$database.";host=".$host, $user, $pass);
  10.  
  11. /********************************************************************************/
  12. // Parameters: filename.csv table_name
  13.  
  14. $table = pathinfo($file);
  15. $table = $table['filename'];
  16.  
  17. /********************************************************************************/
  18. // Get the first row to create the column headings
  19.  
  20. $fp = fopen($file, 'r');
  21. $frow = fgetcsv($fp);
  22. $columns = null;
  23.  
  24. foreach($frow as $column) {
  25. if($columns) $columns .= ', ';
  26. $columns .= "`$column` varchar(250)";
  27. }
  28.  
  29. $create = "create table if not exists $table ($columns);";
  30. $db->query($create)->execute();
  31.  
  32. /********************************************************************************/
  33. // Import the data into the newly created table.
  34.  
  35. $file = $_SERVER['PWD'].'/'.$file;
  36. $q = "load data infile '$file' into table $table fields terminated by ',' ignore 1 lines";
  37. $db->query($q)->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement