How to encode cyrillic in mysql?
what? :-)
I have one problem and I hope you can help me.
A friend of mine has a simple html website and I implemented a little php; CRUD system for articles ... the problem I am facing is placing and getting Cyrillic characters from mysql database.
What I want to achieve is the following:
In the main navigation, there are several separated sections whose names, IDs and the order of the elements I want to place in mysql and then pull the names and put each name as a reference. Names must be Cyrillic characters.
The problem occurs when i, using php function mysql_fetch_assoc
, try to display names that are inserted with cyrillic characters in the database string, sorting the string utf8_general_ci
and I end up ?????
insted from the original characters. If I submit cyrillic characters via the submit form in mysql it shows something like this.
How can I solve this, thanks in advance!? :-)
source to share
if its really mysql fetch assoc messing up you should try: mysql-set-charset
from docs:
Note:
This is the preferred way to change the encoding. 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
source to share
For those who have more complex issues with an outdated upgrade project from versions to PHP 5.6 and MYSQL 5.1 to PHP 7 and MySQL / Percona / MariaDB, etc.
If the project is using utf8_encode ($ value) , you can either try to remove this function from the ready-made value, or use the accepted answer to set UTF-8 encoding for all input data.
--- OR ---
Try replacing utf8_encode ($ value) with mb_convert_encoding ($ value, 'utf-8')
PDO USERS
If you are using PDO, here are two ways 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') into a SQL table using utf8_unicode_ci works for Cyrillic and Umlaut.
source to share