MySQL International Symbols

I can’t figure it out for me.

I have a query that pulls translations of elements on a page. Thus, any number of 15 languages ​​may appear on this page. When I start adding languages ​​like Swedish, everything that has a character like ö causes the whole field to return an empty string.

I checked the encoding in the table and it claims to use UTF-8 , but seeing how it does not work, I got confused.

Here is the query I'm working with:

 SELECT form.form_id, elem.elem_type, elem.elem_name, elem.elem_format, elem.elem_required, trans.trans_label,` trans.trans_description, trans.trans_defaultValue, trans.trans_other, elem.elem_advancedcommand FROM events_form form LEFT JOIN events_form_elements elem ON form.event_id = elem.event_id INNER JOIN events_form_translations trans ON elem.elem_id = trans.elem_id INNER JOIN events_form_languages lang ON trans.lang_id = lang.lang_id WHERE form.form_id = '{$formid}' AND lang.language = '{$language}' ORDER BY elem.elem_sortorder 

Now I tried to do something like:

CONVERT(CAST(trans.trans_description as BINARY) USING latin1) as trans_description,

To make it hide the encoding, but this does not give a result at all.

After receiving the result, it is immediately json_encoded and returned to the user (Ajax Request). I DO NOT think json_encode, since the same problems occur when executing print_r output array.

Also, finally, the system I'm building on is using xPDO, so I'm not too sure what the problem is either.

EDIT: It seems that PHP is returning the correct value or at least the value, for example, this is print_r dump:

 [trans_label] => Ditt f rnamn? [trans_description] => [trans_defaultValue] => First Name 

So it seems that when my json_encode touches this string, this is when it turns the string to zero.

+6
source share
4 answers

Your PDO connection string is encoded. For instance:

 mysql:host=localhost;port=3306;dbname=test;charset=utf8 

This controls the encoding that the database driver will use when it returns the result, and the encoding in which the driver accepts your requests. If you do not specify it, the default encoding will be used. Often latin1 used by default.

You can confirm this by printing the hexadecimal representation of the data using bin2hex in PHP: the return value of ö in förnamn is returned as f6 . If the text was encoded in UTF-8, you would get c3b6 .

+10
source

You did not say anything about coding your web pages.
Do you have this line in the <head> section of your page to force UTF-8 encoding?

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
+3
source

I had problems completing the encoding of letters in my native Icelandic language, but I found a mutual solution for all utf8 letters.

immediately after mysql_select_db and before mysql_query enter the following:

  mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=utf8", $con); 

Where $ con is a connection to mysql

Happy coding.

+1
source

Your answer will be null after conversion due to data type incompatibility. But displaying European or Arabic characters on the page is pretty simple. I had the same problem with the Arabic language, but after several experiments, his work is now beautiful.

If you want to show these European characters on the page (jsp, php, html), first set the page encoding to UTF-8, for example: -

pageEncoding = "UTF-8"

And also you need some changes in your database connection class for utf-8 characters

Use the following code: -

JDBC: MySQL: your_ipaddress ": 3306 /"? + Db + "RequireSSL = false & useUnicode = true & characterEncoding = UTF-8

Hope this helps you.

0
source

Source: https://habr.com/ru/post/920462/


All Articles