Jump to content


Photo

Apostrophe tripping up mysql reading of tab delim file


  • Please log in to reply
1 reply to this topic

#1 Imperialdata

Imperialdata
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 31 March 2006 - 02:55 PM

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 1

The phrase being imported is "didn't treat it as such"

Here's my php 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";
      } 
}
?>

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

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 31 March 2006 - 03:04 PM

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]






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users