1

I took over some old php application with MySQL as database. Inside the database, there are tables including content with localized strings (therefore containing special chars)

Currently there is a PHP application accessing that database. My job is to create an ASP.net (C# codebehind) application that accesses that strings as well. That works, as far as encoding goes.

If I try to access these strings, I do get a kind of encoding problem, like 'Ändern' and 'Prüfzeichen', but only in the ASP.net application. The PHP app sets utf-8 as charset and the strings are perfectly rendered. In the ASP.net application it's gibberish, regardless of the page encoding.

In the MySQL database, the charset for the specified table 'translations' is set to 'latin --cp1252 West European' and collation to 'latin_swedish_ci'.

I can't seem to figure out what PHP apparently does, and ASP.net does not. I traced the php code and could not find any sign of special encoding while getting a string from the database.

The question is, how can I ensure correct encoding inside the ASP.net application without modifying the database, because big changes at the php code are not possible?

Does anybody have a clue?

1
  • 1
    PHP will allow any character set in its strings and they won't get mangled or re-encoded unless you run them through a non-binary safe string function. That's why it works while you don't see any special encoding instructions. Commented May 20, 2010 at 16:59

3 Answers 3

2

The best long-term solution would be to convert the table to use UTF-8 encoding:

ALTER TABLE translations CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

If the data is already in utf-8 format (even though the character set is latin1), you'll need to convert each column to the correct encoding.

This converts a column defined as being latin1 but containing utf8 to a column declared as and containing utf8:

ALTER TABLE translations CHANGE columnNameHere columnNameHere BLOB;
ALTER TABLE translations CHANGE columnNameHere columnNameHere TEXT CHARACTER SET utf8;
Sign up to request clarification or add additional context in comments.

Comments

1

I can't seem to figure out what PHP apparently does,

The PHP app sets utf-8 as charset. For the database connection. With SET NAMES <encoding> query. Where <encoding> is your pages encoding

Comments

0

If finally managed to find way to convert into UTF8.

System.Text.Encoding.UTF8.GetString(System.Text.Encoding.Default.GetBytes("convert me"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.