PHP: Cyrillic (Russian) chars are echoing as Question Marks. Why?

社会主义新天地 提交于 2019-12-10 20:13:30

问题


I have a project with Russian values in the DB. All I need to do is echo them, but it's proving more difficult than anticipated. All russian chars are just printing as question marks. IE: ??? ? ??????? All English chars print just fine for each encoding I've tried.

To simplify my troubleshooting, I playing in my sandbox:

<?php
//header('Content-Type: text/html;charset=koi8-r'); 
//header('Content-Type: text/html;charset=windows-1251');
header('Content-Type: text/html;charset=utf-8');

if(!$link = mysql_connect('localhost', 'id', 'pass')) die('Could not connect: ' . mysql_error());

//mysql_set_charset('ISO-8859-1',$link);
//mysql_set_charset('ISO-8859-5',$link);
//mysql_set_charset('windows-1251',$link);
mysql_set_charset('UTF-8',$link);
//mysql_set_charset('KOI8-R',$link);

if (!$db = mysql_select_db('db', $link)) die ("Can't use DB : " . mysql_error());

$result = mysql_query('SELECT * FROM book');
while($row = mysql_fetch_assoc($result))  {
    echo'<pre>';print_r($row);echo'</pre>';

    $str = $row['russian'];
    $str = mb_convert_encoding($str, "UTF-8", "KOI8-R");
    echo $str;
}
?>

The table field has a koi8r_general_ci collation. I tried changing it to ut8_bin, and utf8_unicode_ci. Neither seemed to help, so I changed it back.

As you can see, I've tried several encodings. I haven't found a fix yet. I'm getting desperate :)

~ Mo

FYI: I am coding in NetBeans on Win7-64. Server= WAMP stack.


回答1:


SOLVED!! The problem came down to a simple hyphen.

As found on http://punbb.ru/viewtopic.php?id=1222 I changed this...

mysql_set_charset('UTF-8',$link);

..to this...

mysql_set_charset('UTF8',$link);

.. and everything works great.

(In the process of trying things, I also came across http://developer.loftdigital.com/blog/php-utf-8-cheatsheet, in case it's helpful.)

I hope this helps someone else at some point in time. ~ Mo

FYI: Here's my sandbox script which works great

/* SANDBOX */
if(!$link = mysql_connect('localhost','user','pass')) die('Could not connect: ' . mysql_error());
mysql_set_charset('UTF8',$link);

if (!$db = mysql_select_db('db', $link)) die ("Can't use $_DB : " . mysql_error());

$result = mysql_query('SELECT * FROM book');
while($row = mysql_fetch_assoc($result))  {
    echo'<pre>';print_r($row);echo'</pre>';  // For Testing ---->
}

mysql_close($link);


来源:https://stackoverflow.com/questions/8392863/php-cyrillic-russian-chars-are-echoing-as-question-marks-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!