Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = 'localhost';
- $user = 'root';
- $pass = '';
- $database = 'ingrid';
- $file = 'ABBREV.csv';
- $db = new PDO("mysql:dbname=".$database.";host=".$host, $user, $pass);
- /********************************************************************************/
- // Parameters: filename.csv table_name
- $table = pathinfo($file);
- $table = $table['filename'];
- /********************************************************************************/
- // Get the first row to create the column headings
- $fp = fopen($file, 'r');
- $frow = fgetcsv($fp);
- $columns = null;
- foreach($frow as $column) {
- if($columns) $columns .= ', ';
- $columns .= "`$column` varchar(250)";
- }
- $create = "create table if not exists $table ($columns);";
- $db->query($create)->execute();
- /********************************************************************************/
- // Import the data into the newly created table.
- $file = $_SERVER['PWD'].'/'.$file;
- $q = "load data infile '$file' into table $table fields terminated by ',' ignore 1 lines";
- $db->query($q)->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement