Jump to content

import all files in directory to mysql


sser

Recommended Posts

Hi everyone,

I'm very new to php/mysql and am trying to upload a whole directory to different tables in a database. But I'm pretty much stuck at the following stage; so if anyone can offer some advice, I would really appreciate it.

 

Thanks!

 

 

<?php

 

$file = "filepath";

  $fcontents = file($file);

 

 

@mysql_select_db(temp) or die( "Unable to select database");

$query = 'CREATE TABLE table( '.

        'field1 INT, '.

        'field2 CHAR(20) NOT NULL, '.

        'field3 CHAR(50) NOT NULL)';

mysql_query($query);

$query1  = "SHOW TABLES";

  mysql_query($query1);

$loadsql = "LOAD DATA INFILE $fcontents INTO table table FIELDS TERMINATED BY " " LINES TERMINATED BY "\n" ";

mysql_query($loadsql) or die(mysql_error());

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/80160-import-all-files-in-directory-to-mysql/
Share on other sites

maybe

$array = array ('filenaname1','filename2','filename3','filename3' etc... );
foreach($array as $val){
$loadsql = "LOAD DATA INFILE $val INTO table table FIELDS TERMINATED BY " " LINES TERMINATED BY "\n" ";
mysql_query($loadsql) or die(mysql_error());
}

Thanks for the reply...

 

I tried to incorporate your suggestion into the code, my problem is also getting the individual tables created for each file...

What I have right now doesn't seem to work...!

 

 

$array = array ("filename1","filename2" etc..);

 

foreach($array as $val){

for($i=0; $i<sizeof($array);$i++){

$query = 'CREATE TABLE $rs[$i]( '.

        'field1 INT, '.

        'field2 CHAR(20) NOT NULL, '.

        'field3 CHAR(50) NOT NULL)';

mysql_query($query);

 

$loadsql = "LOAD DATA INFILE '$val' INTO table '$rs[$i]' FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n' ";

mysql_query($loadsql) or die(mysql_error());

}

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.