1

I need to import bunch of text files(3098) into a database. Each file has to be in different table. My idea is to use a loop that creates a new table for each file. Do i have to use foreach in php to go through all the files or just mysql loop and what name of file am i supposed to put afterLOAD DATA LOCAL INFILE .Also wondering how could i create theese tables (maybe use a variable for their names). Any other ideas to do my task will be helpful.

Niki Karaolis
  • 65
  • 1
  • 12
  • after `LOAD DATA LOCAL INFILE` you put the name of the file you want to import. What other name would you put?? :-) Also `Also wondering how could i create theese folders` - what folders? You haven't mentioned any folders before that remark. – ADyson Aug 12 '16 at 12:28
  • P.S. you could probably write this entirely using MySQL but I suspect PHP might make it a bit easier – ADyson Aug 12 '16 at 12:29
  • @ADyson i meant tables not folders :D. – Niki Karaolis Aug 27 '16 at 09:19
  • But the problem is that i have 3098 different file names. How am i able to loop through all of them. File names are the same, expt the date and time that they are containing. But they dont have any special order. So i am thinking of a way to loop through the folder containing all of the files somehow. @ADyson – Niki Karaolis Aug 27 '16 at 09:24
  • Do you really want 3,098 tables? If all same structure, why not one large table with an identifying field? – Parfait Aug 27 '16 at 12:49
  • Well @Parfait actually yes, it may work out with one large table. – Niki Karaolis Aug 29 '16 at 06:37

1 Answers1

0

Simply use php's glob() to iterate through all .txt files and then run MySQL's LOAD DATA INFILE command on each file. But first, create the destination table with appropriate data type columns in MySQL. Then run import code in PHP. As commented above, this approach assumes all .txt files are of same structure to all be imported into one database table.

MYSQL (one-time run in console or code)

CREATE TABLE destinationTable (
    ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    col1 int(11),
    col2 varchar(255),
    col3 decimal(10,2),
    col4 date,
    ...
);

PHP (using iterative loop; below assumes comma-separated files, adjust to actual delimiter)

// OPEN DATABASE CONNECTION
$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

foreach (glob("/path/to/folder/*.txt") as $file) {

    $sql = "LOAD DATA INFILE '".$file."' 
            INTO TABLE destinationTable 
            FIELDS TERMINATED BY ',' 
            OPTIONALLY ENCLOSED BY '\"' 
            LINES TERMINATED BY '\r\n';";

    $dbh->query($sql);

}

// CLOSE CONNECTION
$dbh = null;
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • That seems nice but i have a problem connecting php and mysql. I am running php in my domain, but only database i could connect to is the corresponding database in phpMyAdmin. The prblem is phpMyAdmin doesnt support `LOAD DATA INFILE` . So if you have any idea how to connect my XAMPP server..... @Parfait – Niki Karaolis Aug 30 '16 at 06:27
  • Phpmyadmin is only a GUI console to MySQL server. Your MySQL settings does not have `LOAD DATA INFILE` enabled. See [this](http://stackoverflow.com/questions/10762239/mysql-enable-load-data-local-infile). Also, you can run this above PHP script in terminal/command line (no browser, apache, server, etc. needed), just like other open-source languages -Perl, Python, R, etc. – Parfait Aug 30 '16 at 18:53