Imperialdata Posted March 31, 2006 Share Posted March 31, 2006 Title says it all really. I am reading in a 4 column tab delimited file saved from MS Excel. It trips up when it reads the last column (description). It is all going fine until the description text has an apostrophe, where my php code gives the 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 't treat it as such" ')' at line 1The phrase being imported is "didn't treat it as such"Here's my php code:[code] $fcontents = file ('startup.txt'); for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i], '\t'); $arr = explode("\t", $line); $sql = "insert into startup values ('". implode("','", $arr) ."')"; mysql_query($sql); echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br>\n"; } }?>[/code]The MySQL database has 4 cols, all are VARCHAR. I have tried changing the problem column to TEXT with the same error.Thanks in advance Quote Link to comment Share on other sites More sharing options...
toplay Posted March 31, 2006 Share Posted March 31, 2006 Backslash the quotes using addslashes(), mysql_escape_string(), or mysql_real_escape_string() before inserting/updating a table. For instance, change this line:$arr = explode("\t", $line);to this:$arr = explode("\t", addslashes($line));Read up on those functions at [a href=\"http://www.php.net\" target=\"_blank\"]http://www.php.net[/a] 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.