Trevor
Trevor

Reputation: 16116

Textarea value saved to database weird characters

Information from a text area is being saved to my database when a form is submitted. There is one case where the following characters are showing up in the field.

E.g.

• Text

And if I pull the data out of the database using java it looks like:

�?� Text

I'm guessing it has something to do with character encoding. Would adding this tag <meta charset="UTF-8"> to the top of my page fix it? Or is there something I can do with the text area to clean it up before saving the value to a database?

This is not something I know how to reproduce on my own I just saw it while monitoring the database. Any information or enlightenment on the subject would be much appreciated.

Upvotes: 1

Views: 970

Answers (1)

Roman Holzner
Roman Holzner

Reputation: 5928

Maybe, you have to set the MySQL connection to UTF-8.
For Example for PHP/MySQLi and MySQL:

$utf8_mysqli = $mysqli ->set_charset("utf8");

Quick example why you shouldn't encode with htmlentities() or the like:

Without encoding:

> INSERT INTO `table` (test) VALUES ("Max Ämerich"), ("50 €");
--------------------
| test        | ai |
====================
| Max Ämerich |  1 |
|   50 €      |  2 |
====================

With encoding

> INSERT INTO `table` (test) VALUES ("Max &auml;merich"), ("50 &euro;");
-------------------------
| test             | ai |
=========================
| Max &auml;merich |  1 |
|   50 &euro;      |  2 |
=========================

Now try a SELECT

SELECT * FROM table WHERE test = "50 €";

If the values are encoded, this will return 0 rows.

Upvotes: 2

Related Questions