sser Posted December 4, 2007 Share Posted December 4, 2007 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/80160-import-all-files-in-directory-to-mysql/ Share on other sites More sharing options...
fenway Posted December 4, 2007 Share Posted December 4, 2007 You can't call your table "table". Quote Link to comment https://forums.phpfreaks.com/topic/80160-import-all-files-in-directory-to-mysql/#findComment-406269 Share on other sites More sharing options...
sser Posted December 4, 2007 Author Share Posted December 4, 2007 How would you let it loop over all the files in the directory...? Quote Link to comment https://forums.phpfreaks.com/topic/80160-import-all-files-in-directory-to-mysql/#findComment-406422 Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 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()); } Quote Link to comment https://forums.phpfreaks.com/topic/80160-import-all-files-in-directory-to-mysql/#findComment-406443 Share on other sites More sharing options...
sser Posted December 4, 2007 Author Share Posted December 4, 2007 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()); } } Quote Link to comment https://forums.phpfreaks.com/topic/80160-import-all-files-in-directory-to-mysql/#findComment-406468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.