MySQL – encoding

Databases sometime does not recognizes what encoding you are using on your site, so it is necessary to specify in the database, how communication will take place.

Database setting

Primarily, it is good to set encoding database tables, and their respective columns of text, which you are using in your application. It is better to create a new database and table then set the attribute “comparison”, which supplies the encoding before their use. If you did not do so already, you can try to change the attributes of already created tables. And beware, data that contain tables may be corrupted.

Setting encoding of existing databases and tables can be made via phpMyAdmin click on the name of the database (tables) and selecting the Edit tab in the upper menu. Then select appropriate encoding in the box “comparison”. For each column of the table do the same in their editing.

Script setting 

If the site displays text in the wrong encoding, the essence of the problem lies in a different encoding used by your script, when communicating with MySQL, and which is used in your site. The default encoding in combination with our MySQL database is UTF-8, MySQL will returns data in UTF-8 (no matter what encoding the data is stored in the database because the conversion is done on encoding).

If your site uses a different encoding (according to META content-type header), you need to tell PHP that you want the MySQL data in the given encoding.

For example. If your site publishes in ISO 8859-2 (or Latin2):

<head>
 <meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
 <title>exaple header</title>
</head>

Then this is necessary also to tell PHP to communicate with MySQL. You can do this by SQL command SET NAMES. For example:

$link = mysql_connect("wm1.wedos.net", "user_test", "pass_test");
mysql_select_db("test_db", $link);
mysql_query("SET NAMES latin2");