brooksh Posted January 24, 2008 Share Posted January 24, 2008 I'm trying to import my csv but how do I tell it that it is ENCLOSED BY '\"' and LINES TERMINATED BY '\r\n' My CSV looks like this //line1 "OfficeID","Name","Phone","Extension","Address","City","State","Zip Code","Country","Fax Number","Email Address","URL","Short ID" //line2 1,"Exact","789-9400","","Aleshia, 421 Oak","City","State","33333","","838-2117","","","ext" //line3 2,"Martin","667-9800","","5 South Washington","City","State","33333","","456-8201","","","MPR" Here is my code, but because the first line is enclosed with "" and the lines there after are enclosed with " it messes up. $file = "file.csv"; $tbl = "organizations"; $handle = fopen ($file, "r"); mysql_query($sql); while (!feof ($handle)) { $line = fgets($handle, 1000); list($OfficeID,$Name,$Phone,$Extension,$Address,$City,$State,$Zip,$Country,$Fax,$Email,$URL,$ShortID) = split("'","\'",$line); $sql = "insert into $tbl (OfficeID,Name,Phone,Extension,Address,City,State,Zip,Country,Fax,Email,URL,ShortID) values ('$OfficeID', '$Name', '$Phone', '$Extension', '$Address', '$City', '$State', '$Zip', '$Country', '$Fax', '$Email', '$URL', '$ShortID')"; mysql_query($sql); } Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/ Share on other sites More sharing options...
Donovan Posted January 24, 2008 Share Posted January 24, 2008 Here is how I did mine. It was test results from a university scantron that was a Excel file. I saved as a CSV and it works well. $file_name = $HTTP_POST_FILES['file_source']['tmp_name']; //Create the Import CSV table $sql = "CREATE TABLE IF NOT EXISTS ".$prefix."$table_name ( `ReportNumber` int(3) NULL, `Student_ID` varchar(9) NULL, `Name_Last` varchar(50) NULL, `Name_First` varchar(30) NULL, `Total_RS` tinyint(4) NULL, `Total_Percent` tinyint(3) NULL, `Percentile` tinyint(3) NULL, `Subtest_RS` tinyint(3) NULL, `Subtest_Percent` tinyint(3) NULL )"; $result = $db->sql_query($sql); if (!$result) {echo("<p>Error performing query: " . mysql_error() . "</p>");} $sql = "LOAD DATA LOCAL INFILE '".mysql_real_escape_string($file_name)."' INTO TABLE ".$prefix."$table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r'"; if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");} $result = $db->sql_query($sql); Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-447927 Share on other sites More sharing options...
brooksh Posted January 24, 2008 Author Share Posted January 24, 2008 sorry but it didn't work, and I did not get any errors. Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448033 Share on other sites More sharing options...
aztec Posted January 24, 2008 Share Posted January 24, 2008 Hello I used a program called SQLyog which will allow you to inport a CVS file. Sorry I cannot be more specific but it was some time ago. I know it worked with an Excel sheet I needed to get into mysql, you can download it for free at some sites. I also remember that when I searched there were other programs that will do what you require. Regards Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448034 Share on other sites More sharing options...
brooksh Posted January 24, 2008 Author Share Posted January 24, 2008 It needs to be done by a script via cron. Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448118 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 split by , strip the " Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448154 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 change = split("'","\'",$line); to = split(",",str_replace('"','',$line)); Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448163 Share on other sites More sharing options...
brooksh Posted January 24, 2008 Author Share Posted January 24, 2008 Thank you. What about skipping line 1? Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448185 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 read line 1 before entering the loop Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448190 Share on other sites More sharing options...
brooksh Posted January 24, 2008 Author Share Posted January 24, 2008 How could I do that? I tried fopen ($file[0], "r"); and I tried echo $file[0]; Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448203 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 mysql_query($sql); while (!feof ($handle)) { $line = fgets($handle, 1000); to mysql_query($sql); $line = fgets($handle, 1000); while (!feof ($handle)) { $line = fgets($handle, 1000); Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448224 Share on other sites More sharing options...
craygo Posted January 24, 2008 Share Posted January 24, 2008 use fgetcsv instead <?php $row = 0; $handle = fopen($filename, "r"); while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){ $row++; // will skip first row which contains field names if($row > 1){ $sql = "insert into $tbl (OfficeID,Name,Phone,Extension,Address,City,State,Zip,Country,Fax,Email,URL,ShortID) values ('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', '".$data[7]."', '".$data[8]."', '".$data[9]."', '".$data[10]."', '".$data[11]."', '".$data[12]."')"; } } ?> Ray Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448238 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 SSDN Link to comment https://forums.phpfreaks.com/topic/87575-solved-csv-import-into-mysql-database-problem/#findComment-448260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.