How to encode cyrillic in mysql?

后端 未结 4 1179
挽巷
挽巷 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: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.

提交回复
热议问题