techker Posted December 1, 2011 Share Posted December 1, 2011 Hey guys i have to choices either .txt or .xls or even .htm file to upload to my MYSQL DB.. we have a school software that export all the students in .txt-.htm-.xls files.. so im trying to see witch file would be the easiest to work with... so i got this going $file = "LT.txt"; $fp = fopen($file, "r"); $data = fread($fp, filesize($file)); fclose($fp); $output = str_replace("\t|\t", "|", $data); $output = explode("\n", $output); foreach($output as $var) { $tmp = explode("|", $var); $Ecole = $tmp[0]; $N_Fiche = $tmp[1]; $Nom = $tmp[2]; $Prenom = $tmp[3]; $Code_P = $tmp[4]; $sql = "INSERT INTO Etudiant SET Ecole='$Ecole', N_Fiche='$N_Fiche', Nom='$Nom',Prenom='$Prenom',Code_P='$Code_P'"; mysql_query($sql); } echo "Done!"; So it works but it only does the first row ..so i open the txt file it noticed that there is no coma or anything sepereating the data..just a space... like: 263 7924004 tim allan ABtim16059609 801 3 1 116263560 Quote Link to comment https://forums.phpfreaks.com/topic/252171-txt-file-to-mysql-help/ Share on other sites More sharing options...
fenway Posted December 1, 2011 Share Posted December 1, 2011 Formatted data is always better -- xls is preferable -- LOAD DATA INFILE is your firend. Quote Link to comment https://forums.phpfreaks.com/topic/252171-txt-file-to-mysql-help/#findComment-1293093 Share on other sites More sharing options...
techker Posted December 1, 2011 Author Share Posted December 1, 2011 ya..i tryed.. mysql_connect($db_host,$db_username,$db_password); @mysql_select_db($db_database) or die( "Unable to connect to database."); $fname = $_FILES['sel_file']['name']; $table = "Etudiant"; $sql = "LOAD DATA INFILE '$fname.xls' REPLACE INTO $table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES "; what if in the exported file there is no field id.. i needed to add the fields to the exported csv ,.. Quote Link to comment https://forums.phpfreaks.com/topic/252171-txt-file-to-mysql-help/#findComment-1293275 Share on other sites More sharing options...
fenway Posted December 3, 2011 Share Posted December 3, 2011 What/ Quote Link to comment https://forums.phpfreaks.com/topic/252171-txt-file-to-mysql-help/#findComment-1293915 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.