2

我正在尝试制作一个显示日文字符的页面。这些日文字符存储在我的数据库中。我已经完成了我的研究并尝试了许多给出的建议,但我的日文字符仍然作为问号返回。目前,这是我的代码的外观:

<?php


 $con=mysqli_connect("host", "user", "pw", "db");   
if (!$con)
{
    die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}   
}
?>

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

<body>
<?php
    mb_internal_encoding('UTF-8'); ?>
    <center>
        <div id="fb-root"></div>
        <form method="POST" id="myForm">
            <h1>
                <?php 
                $title = mysqli_query($con, "SELECT `title` FROM titles WHERE c1='a' or c2 ='b'") or die("ERROR: ". mysqli_error($con));
                $title_str = mysqli_fetch_array($title);

                $mystr = mb_convert_encoding($title_str[0], "UTF-8");
                echo "test: ".$mystr;
                        ?>
        </h1><br/>
    </form>
</body>

我已经检查了我的字符编码是否有效,如果我将 $title_str[0] 替换为日语字符串,它是否有效。我在这里错过了什么吗?我不确定为什么编码适用于我手动输入的字符串,而不是我从数据库中获得的字符串。

4

1 回答 1

2

连接到 MySQL 后尝试使用 SET NAMES utf8:

$con=mysqli_connect("host", "user", "pw", "db");   
if (!$con)
{
    die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}

mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));

正如手册所说:

SET NAMES 指示客户端将使用什么字符集将 SQL 语句发送到服务器......它还指定服务器应该使用的字符集将结果发送回客户端。

之后就没有必要使用了mb_convert_encoding

于 2013-05-20T08:51:26.383 回答