How to encode cyrillic in mysql?

后端 未结 4 1174
挽巷
挽巷 2021-01-14 08:14

what\'s up? :-)
I have one problem and i hope you can help me with it.

One friend of mine have a simple solid html website and i implemented little php; CRUD sys

相关标签:
4条回答
  • 2021-01-14 08:40

    Make sure you call this after connecting to database.

    mysql_query("SET NAMES UTF8");
    

    Also make sure that HTML file has charset meta tag set to UTF-8 or send header before output.

    header("Content-Type: text/html; charset=utf-8");
    
    0 讨论(0)
  • 2021-01-14 08:47

    if its really mysql fetch assoc messing up you should try: mysql-set-charset

    from the docs:

    Note:

    This is the preferred way to change the charset. Using mysql_query() to execute SET NAMES .. is not recommended.

    also make sure your files are saved as utf8 and check iconv_set_encoding / iconv_get_encoding

    0 讨论(0)
  • 2021-01-14 08:50

    For anyone having more complex issues with legacy project upgrades from versions before PHP 5.6 and MYSQL 5.1 to PHP 7 & Latest MySQL/Percona/MariaDB etc...

    If the project uses utf8_encode($value) you can either try removing the function from the value being prepared and use the accepted answer for setting UTF-8 encoding for all input.

    --- OR ---

    Try replacing utf8_encode($value) with mb_convert_encoding($value, 'utf-8')

    PDO USERS

    If you are using PDO here are two ways how to set utf8:

    $options = [
        \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
    ];
    
    new \PDO($dsn, $username, $passwd, $options);
    

    --- OR ---

    $dsn = 'mysql:host=localhost;charset=utf8;'
    
    new \PDO($dsn, $username, $passwd);
    

    I can confirm that mb_convert_encoding($value, 'utf-8') to SQL table using utf8_unicode_ci works for Cyrillic and Umlaut.

    0 讨论(0)
  • 2021-01-14 08:54

    I had the same problem until I encoded the 'Collation' column in my table to 'utf8_bin'.

    0 讨论(0)
提交回复
热议问题