0

I'm programming a system so an user can edit what he posts. Simplified it's a textarea/input field which stores in a database and a page that retrieves it. The problem is, I think the encoding isn't okay, because strings are stored in the database like "é" or something (phpmyadmin view).

Insert page:

  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  2. I insert mysql_real_escape_string($_POST['field']);

Output page:

  1. Object from database.
  2. htmlspecialchars($object->field);

But expected is: Output page:

  1. Object from database.
  2. htmlentities($object->field);, right?

Why isn't the data stored in MySql properly?

4 Answers 4

4

If your database encoding is set to utf-8, you need to set the tranfer encoding to utf-8, too. To do that you have to query

SET NAMES utf8;

before inserting into the table.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 This is necessary to see the correct encoding in phpMyAdmin. However, it is usually not necessary to get the same out of the DB that has been put in.
That is true Andre, but if you connect to the database using the wrong character set then you may actually be storing mis-encoded data in the database table. This is obviously undesirable.
2

Is the database connection UTF-8 ? Bear in mind, that's not the default encoding, you have to explicitly set it yourself.

Comments

1

You have to tell htmlentities that you feed it UTF-8:

htmlentities($text, ENT_COMPAT, "UTF-8");

Otherwise it assumes it gets ISO-8859-1.

1 Comment

Maybe i'm looking for something different, because i've placed something in the database ("Testé"), and it's displayed okay with htmlentities($text); How can I insert "Testé" correctly without the help of phpmyadmin?
0

This site http://developer.loftdigital.com/blog/php-utf-8-cheatsheet should give you an idea of whats needed for UTF-8 in PHP and MySQL.

Oh and dont forget to use these after connecting to your DB:

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");

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.