iceblox Posted July 19, 2007 Share Posted July 19, 2007 Hi guys, Im having trouble using this csv to mysql script everytime i excute the file on my server it says the follwoing 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 'table (Date, Price) VALUES ('11-May,89.99 ', '')' at line 1 This is the code.. <?php $connection = mysql_connect('localhost','username','pass'); mysql_select_db('mobile'); $file = 'DatePrice.csv'; //file name here //we read the CSV file here $lines = file($file); //now we take each line and explode it then insert to DB foreach ($lines as $line) { $cols = explode(';', $line); mysql_query("INSERT INTO table (Date, Price) VALUES ('$cols[0]', '$cols[1]')")or die(mysql_error()); } echo mysql_affected_rows(); ?> Not to sure what is causing the error. Any one got any ideas? Link to comment https://forums.phpfreaks.com/topic/60737-solved-csv-to-mysql-script-php/ Share on other sites More sharing options...
thedarkwinter Posted July 19, 2007 Share Posted July 19, 2007 Hmm A few suggestions: 1. Is your table name really "table"??? Off the top of my head i thought that was a reserved word?? try "mytable" or something. 2. you should always trim values when reading: $cols = explode(';', trim($line)); 3. What are your column definitions, do you specify NOT NULL for price? Hope something there helps you... cheers, tdw Link to comment https://forums.phpfreaks.com/topic/60737-solved-csv-to-mysql-script-php/#findComment-302225 Share on other sites More sharing options...
matto Posted July 19, 2007 Share Posted July 19, 2007 I think you will find your CSV delimiter is actually a , not a ; Link to comment https://forums.phpfreaks.com/topic/60737-solved-csv-to-mysql-script-php/#findComment-302229 Share on other sites More sharing options...
iceblox Posted July 19, 2007 Author Share Posted July 19, 2007 Thanks to both of you! Followed both of your advice and it worked a treat! Turns out table isnt to be used as an actually table name! Who would of thought it LOL! Thanks Again guys! Link to comment https://forums.phpfreaks.com/topic/60737-solved-csv-to-mysql-script-php/#findComment-302440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.