Problems with Php, Mysql and UTF8

The problem is simple and annoying.


I'm just trying to print a list of names compiled from my mysql database.

PHP files are saved in utf8, the database and tables are configured to use utf8. However, "Γ₯, Γ€, ΓΆ", for example, displays as. I can’t believe that I still have this problem.

enter image description here

Of course, Latin1 solves the problem. The thing is, I have to use utf8, since I am doing json_encode to send data in ajax-script.

enter image description here

Any idea what on earth might be wrong?

Should I convert the data to utf8 before returning it, maybe? It seems strange that I should ...

+3
source share
2 answers

Convert utf8_general_ci to utf8_unicode_ci ...

Try to run SET NAMES UTF8 request after connecting to the database ...

 function connect($server, $database, $username, $password, $charset = "UTF8"){ $link = mysql_connect($server, $database, $password); if(!$link){ die("Unable to connect to database server."); } mysql_selectdb($database); if(function_exists("mysql_set_charset")){ mysql_set_charset($charset, $link); }else{ mysql_query("SET NAMES $charset"); } } 

Also make sure you have this (or via header ()) in your HTML ...

 <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
+4
source

Two things to do ...

  • Make sure your HTML header sends the correct encoding:

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

  • Use utf_encode() when adding names to an array. Your line should be

    $guests[] = array_map('utf8_encode', $row);

0
source

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


All Articles