Cannot store UTF8 characters in MySQL

前端 未结 6 1142
抹茶落季
抹茶落季 2021-01-06 14:38

Cannot find the reason why I am unable to store in a MySQL database characters like ţ, î, ş.

My table definition is:

CREATE TABLE IF NOT EXISTS `gen_         


        
6条回答
  •  星月不相逢
    2021-01-06 15:22

    Expanding my comments into an answer:

    It seems that you have set up things correctly, and are only stuck on inserting a string literal to the database. To do that successfully you must also ensure that your text encoding for the saved PHP script is also UTF-8.

    Most decent editors will let you know which encoding you are currently working with and can also save as (i.e. convert between) different encodings (even Notepad does this today). However, as a quick check you can add the character to your file somewhere and save it. If the file size changes by 1 or 2 bytes instead of 3, you are not on UTF-8 and you need to convert the file to that encoding.

    Other than that, when receiving text as input from the browser your code should handle it just fine.

    Note: While using a tag to set the encoding for your page should be sufficient, it's better if you do this with an HTTP header from PHP like this:

    header('Content-type: text/html; charset=utf-8');
    

提交回复
热议问题