2

i've set the meta and check mysql appears to be on utf 8 already, what seems to be the issue here?

HTML

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

enter image description here

PHP DB connection

$DB_NAME = 'ssl';
$DB_HOST = 'localhost';
$DB_USER = 'dbuser';
$DB_PASS = 'dbpass';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);


// $mysqli->character_set_name();
mysqli_set_charset($mysqli,"utf8");


if (mysqli_connect_errno())
{
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Querying for output

$query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
        $row = mysqli_fetch_array($query);

Display

<h1><?php echo translate("In-Kind Donation"); ?></h1>

tried setting header('Content-Type: text/html; charset=UTF-8') as well but the result is still the same

enter image description here

Edit: add in translate function info

 function translate($phrase) {

    if(!isset($_SESSION)) 
    { 
        session_start(); 
    }

    require 'db/mysql.php';

        $_GET['lang'] = $_SESSION['lang'];
        $lang_session = $_GET["lang"];

        if($lang_session == 'CN') {
            $translate_from = 'EN';
            $translate_to = 'CN';
        } 
        else {
            $translate_from = 'CN';
            $translate_to = "EN";
        } 

        $query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
        $row = mysqli_fetch_array($query);

        if(isset($row['lang']) && $row['lang'] !== ""){

            return $row['lang'];
        }
        else{
           return $phrase;
        }
    }
  • What does the `translate` function do? – Peon Jan 04 '17 at 14:17
  • it queries the db for matching string and return the result else return the original string... i've updated the question for that function code –  Jan 04 '17 at 14:22
  • If the data is stored nicely in the database, then I'm guessing it's a PHP header missing. – Qirel Jan 04 '17 at 14:24

2 Answers2

3

Some Chinese characters need 4 bytes for encoding. MySQL's CHARACTER SET uf8 stops at the 3-byte encodings. Change to utf8mb4

mysqli_set_charset($mysqli,"utf8");
mysqli_set_charset($mysqli,"utf8mb4");

Your <meta ... charset=UTF-8" /> tag is correct as written.

(This also applies to Emoji.)

Milap
  • 6,915
  • 8
  • 26
  • 46
Rick James
  • 135,179
  • 13
  • 127
  • 222
0

You may also need to set mb_internal_encoding("UTF-8");

  • tried but it's still the same however when using and adding an extra space at the end it's displaying correctly - mb_strtolower($row['lang'].' ', 'UTF-8'); –  Jan 04 '17 at 16:59