-4

I have MySQL field which contains plain text Cyrillic characters (ex. Широка поляна). Collation is utf8_general_ci.

When I pull out this content with MySQL query and try to output it with php I always get ???? symbols. HTML encoding is utf8, document encoding is utf8, mb_detect_encoding() shows ASCII for the string but none of the PHP / MySQL convert functions turns it into something readable.

Prix
  • 19,417
  • 15
  • 73
  • 132
Kyobul
  • 759
  • 1
  • 7
  • 17

4 Answers4

3

For the outdated mysql driver it have to be

mysql_set_charset('utf8');

for mysqli

$mysqli->set_charset('utf8');

for PDO you have to set encoding in DSN:

$dsn = "mysql:host=localhost;dbname=test;charset=utf8";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

You most likely do very common mistake by not setting connection encoding. It's usually done in my.ini config file, but IMHO the better way is to always enforce this in your code. To do so, just execute this query:

SET NAMES encoding;

i.e. for utf8 it would be:

SET NAMES utf8;

You do this just after you connect to database and do it once per connection.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

You have to set charset for your database connection. For this reason you can use mysql_set_charset function.

mysql_set_charset('utf8',$link1); 

More information: http://php.net/mysql_set_charset

Ahmad
  • 5,551
  • 8
  • 41
  • 57
0

Use in first line of php file

 header('Content-Type: text/html; charset=utf-8');
alok.kumar
  • 380
  • 3
  • 11