mbk Posted March 5, 2010 Share Posted March 5, 2010 Hi, I am trying to parse a csv file and insert the contents into a mysql table. I could use the insert local file option, but I dont want all the columns importing. I have the following code, but this doesnt seem to work. <?php $table = 'suppliers'; $username = "user"; $pass = 'pass'; $db = mysql_connect("localhost", $username, $pass); mysql_select_db("testdb",$db); $empty_table = "TRUNCATE TABLE $table"; mysql_query($empty_table) or die (mysql_error()); $file_handle = fopen("file.csv", "r"); while (!feof($file_handle) ) { $tmp = fgetcsv($file_handle, 8192); $category = ($tmp[0]); $supp_pn = ($tmp[1]); $desc = ($tmp[3]); $help = ($tmp[5]); $sql = "INSERT INTO $table SET category='$category', supp_pn='$supp_pn', desc='$desc', $help='help'"; mysql_query($sql)or die (mysql_error()); } fclose($file_handle); ?> An example of the data in the csv is below - "","FUNDHD ","","FUNDHD ","",0.00,0.00,0 "[Pending Assignment]","ATS0057 "," ","AT-FS705LE-30 ","Hama UK Ltd ",17.31,29.80,0 "[Pending Assignment]","MARKETING "," ","MARKETING ","Hama UK Ltd ",20000.00,30000.00,0 Can someone help pls? Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/ Share on other sites More sharing options...
harristweed Posted March 5, 2010 Share Posted March 5, 2010 NOT $sql = "INSERT INTO $table SET category='$category', supp_pn='$supp_pn', desc='$desc', $help='help'"; mysql_query($sql)or die (mysql_error()); BUT $sql="INSERT INTO $table (category, supp_pn, desc, help)VALUES('$category', '$supp_pn', '$desc', '$help' )"; Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/#findComment-1021845 Share on other sites More sharing options...
mbk Posted March 5, 2010 Author Share Posted March 5, 2010 Thanks for the quick reply - I made the amend and now get the following error.. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc)VALUES('', 'FUNDHD ', 'FUNDHD ')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/#findComment-1021850 Share on other sites More sharing options...
harristweed Posted March 5, 2010 Share Posted March 5, 2010 Lets see the new insert statement Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/#findComment-1021855 Share on other sites More sharing options...
mbk Posted March 5, 2010 Author Share Posted March 5, 2010 sorry... $sql="INSERT INTO $table (category, supp_pn, desc)VALUES('$category', '$supp_pn', '$desc')"; mysql_query($sql)or die (mysql_error()); MySQL version 5.0.75 Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/#findComment-1021856 Share on other sites More sharing options...
harristweed Posted March 5, 2010 Share Posted March 5, 2010 desc is a reserved word in MYSQL you need to change the field name. Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/#findComment-1021860 Share on other sites More sharing options...
mbk Posted March 5, 2010 Author Share Posted March 5, 2010 Thanks Harris - all sorted now. Quote Link to comment https://forums.phpfreaks.com/topic/194223-help-parsing-csv-file/#findComment-1021865 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.