0

I'm getting encoding problem when I insert names in a mySQL table, that contains special characters, like "ö", "ä" etc. For example, the word "Öl" becomes "öl".

I've tried to write the names to a text file and then they show up properly.

I've tried to insert the names in phpMyAdmin using SQL-statements, and that works good as well.

Now I found a solution in setting mysql_query('SET NAMES utf8;'); before the insert query. Is this how it should be done, or is there a better way?

1

1 Answer 1

1

yeah running the SET NAMES utf8; is needed to make the MySQL know that the client connection is using ut8 while sending the data. You can though now define it inside the PDO connection (if you are using PDO for connecting to the MySQL).

If running PHP version older than 5.3.6 then you can use the following code:

$pdo = new PDO(
'mysql:host=mysql.example.com;dbname=example_db',
"username",
"password",
array(PDO::MYSQL\_ATTR\_INIT\_COMMAND => "SET NAMES utf8"));

else use the following:

$pdo = new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass');
Sign up to request clarification or add additional context in comments.

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.