2

I'm trying to make a page that displays japanese characters. These japanese characters are stored in my database. I've done my research and tried many of the suggestions given but my japanese characters still return as question marks. Currently, this is how my code looks:

<?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>

I've already checked if my character encoding works and it does work if I replace $title_str[0] to japanese string. Am I missing something here? I'm not sure why the encoding works on strings I manually input but not the one I got from the database.

3
  • Are you sure the field of the string in the database is capable of holding UTF8? Commented May 20, 2013 at 7:31
  • Yes, it is. All my strings inside my database are japanese characters Commented May 20, 2013 at 7:53
  • If from_encoding is not specified, the internal encoding will be used. from PHP.org on mb_convert_encoding. I guess this is probably what you intended, but try specifying it exactly as in the database (Pick one of these and chuck it in the final optional argument of mb_convert_encoding php.net/manual/en/mbstring.supported-encodings.php). I'm guessing the ?s are from mb_convert_encoding being unable to decypher the input to UTF-8 Commented May 20, 2013 at 8:09

1 Answer 1

2

Try using SET NAMES utf8 after connecting to MySQL:

$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));

As the manual says:

SET NAMES indicates what character set the client will use to send SQL statements to the server... It also specifies the character set that the server should use for sending results back to the client.

After that there is no need to use mb_convert_encoding.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. This worked great. I did change something on your answer. I added single quotes for UTF8 and capitalized it since it didn't work without the single quotes or in lower case. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.