0

我有下面的mysql连接:

$dbLink = mysql_connect('127.0.0.1', 'root', '');
mysql_set_charset('utf8', $dbLink);
mysql_select_db('test', $dbLink);
mysql_set_charset('utf-8'); 

1 周前它工作正常,但突然它没有显示存储在数据库中的 utf-8 或阿拉伯语或 unicode 内容。但现在即使在数据库中的字符已经改变,就像سید احمد Ø´Ûید Ú©ÛŒ صحیح 在 php 中显示一样。在一切都完美之前。并且内容显示正确。

即使是现在,当我在数据库中创建一个新条目时,它的显示效果很好,但是旧条目出了点问题,请帮助......

我试过设置名称,mysql_set_charset &<meta http-equiv="Content-Type" content="text/html; charset=utf-8">但什么也没发生。请帮忙,否则我的网站将被挂断......

4

1 回答 1

0

使用 MYSQLi,因为现在不推荐使用 MYSQL。

要设置 charset utf-8,请使用 mysqli_set_charset(),如下所示:

$dbLink = mysqli_connect('127.0.0.1', 'root', '');
mysqli_set_charset($dbLink , 'utf8');
mysqli_select_db($dbLink, 'test');

如果由于任何原因它仍然显示错误的字符,请将其添加到文档的开头:

// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');

// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');

更新

从您的数据库中选择一些内容并使用输出值iconv()并查看它是否显示 wright 字符集:

<?php

    $dbLink = mysqli_connect('127.0.0.1', 'root', '');
    mysqli_set_charset($dbLink , 'utf8');
    mysqli_select_db($dbLink, 'test');

    function get_table_contents(){
        global $dbLink;
        $contents = array();

        $query = "SELECT * FROM ||YOUR TABLE HERE||";
        $result = mysqli_query($connection, $query);

        while($content = mysqli_fetch_assoc($result)){
            $contents[] = $content;
        }
        return $contents;
    }

    $data = get_table_contents();

?>
<html>
   <head></head>
   <body>
      <?php echo iconv(mb_detect_encoding($data[0]['||YOUR COLUMN||'], "UTF-8,ISO-8859-1"), "UTF-8", $data[0]['||YOUR COLUMN||']); ?>
   </body>
</html>
于 2013-10-13T00:03:03.377 回答