gregaspen Posted August 25, 2009 Share Posted August 25, 2009 I think I'm close but the script below will not loop... therefore it only writes a single line to the database. Help! -------------------- <?php $databasehost = "localhost"; $databasename = "$db"; $databasetable = "$table"; $databaseusername ="$user"; $databasepassword = "$pass"; $fieldseparator = ","; $lineseparator = "\n"; $csvfile = $_FILES['dataupload']['tmp_name']; if (!file_exists($csvfile)){ echo "File not found. Make sure you specified the correct path.\n"; exit; } $file = fopen($csvfile,"r"); if (!$file){ echo "Error opening data file.\n"; exit; } $size = filesize($csvfile); if(!$size){ echo "File is empty.\n"; exit; } $csvcontent = fread($file,$size); fclose($file); $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error()); @mysql_select_db($databasename) or die(mysql_error()); $lines = 0; $queries = ""; $linearray = array(); $lineexp = explode($lineseparator,$csvcontent); foreach($lineexp as $line){ $lines++; $line = trim($line," \t"); $line = str_replace("\r","",$line); $line = str_replace("'","\'",$line); $linearray = explode($fieldseparator,$line); $linemysql = implode("','",$linearray); $query = "INSERT INTO $databasetable VALUES('$linemysql')"; $upload = @mysql_query($query); } if ($upload){ header ("location:index.php?count=$lines"); exit(); } else { header ("location:index.php?error=1"); exit(); } ?> Quote Link to comment Share on other sites More sharing options...
merck_delmoro Posted August 25, 2009 Share Posted August 25, 2009 that's because you implode the $linearray it will join all the data inside the $linearrray and insert it into the database Quote Link to comment Share on other sites More sharing options...
gregaspen Posted August 25, 2009 Author Share Posted August 25, 2009 if I place echo ($linemysql); exit(); after implode. I only get the first line of data, properly formatted albeit. I should think I would get the entire database with the echo command. Am I on dope? OH WAIT! I am on dope. I should get rid of the exit and then comment out both the $query and $upload lines. Yip, now it shows the entire database... properly formatted, except for the end of line. Maybe that is my problem. But not sure how to fix it. anyone, anyone? Quote Link to comment Share on other sites More sharing options...
gregaspen Posted August 25, 2009 Author Share Posted August 25, 2009 Pardon my think out loud process here but after inserting the echo, I shouldn't see an end of line since the foreach loop is only inserting a line at a time. So I'm back where I started. This should work but doesn't. Okay, class. Any hands? Quote Link to comment Share on other sites More sharing options...
attock Posted August 25, 2009 Share Posted August 25, 2009 .CSV file is easy to import into mySQL, its the .XLS file which is a pain! Here is how I import a csv file: // DUMP From .CSV FILE to mysql $query = "load data infile '$target_path' into TABLE `my_table_raw` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES;"; if (mysql_query($query)) { // success } else{ // error } Once i have the data in raw table, i do all kinds of data verification on that before movig it into a final table. Hope it helps .. and why do I have to enter the spam-verfication code ? :S Quote Link to comment 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.