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
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());
}

Link to comment
Share on other sites

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());

}

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.