• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Strange characters being inserted into SQL Server 2005 in place of normal ones, PHP

Reviewing the contents of a database on SQL Server 2005, I've found completely at random, strange characters often in place of the single quote. I have the single quote escaped as anyone would by replacing it with regular expressions with two single quotes.  The last one I've found I'm wondering if it because used an accented character it is "exposé", is this because they used an accented e? That is what I would assume had I not found similar strange character combinations where single quotes should be in many records, but not all, even the same word in the same field.

It is being sent POST data to PHP 5, I've gone through the script and I don't use htmlentities etc.

Any ideas?
1 Solution
That seems to be an utf-8 (multi-byte) encoding.
You have to convert it to latin1 if this is your native encoding (french ?), or update your database/table encodings accordingly !
MS SQL Server uses the UCS-2 encoding scheme for Unicode, try using the mb_convert_encoding PHP function http://www.php.net/mb_convert_encoding to convert the data encoding before you insert it into the database, e.g.:

// Convert encoding to USC-2 before storing the data into the database
$sText = mb_convert_encoding($sText, "UCS-2", "UTF-8");

Then when you retrieve the data from the database convert it back to UTF-8:

$sText = mb_convert_encoding($sText, "UTF-8", "UCS-2");

The unusual character you noticed instead of a single quote might potentially be the apostrophe from Microsoft Word.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now