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
Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.