Jump to content

Apostrophe tripping up mysql reading of tab delim file


Imperialdata

Recommended Posts

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:

[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
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.