用数据库中的php显示日语字符

时间:2018-06-23 23:43:05

标签: php phpmyadmin

PHPMyAdmin使用COLLATION =:utf8mb4_unicode_ci完美显示日语字符,但仅显示????在浏览器中。

我在HTML标头中使用了正确的UTF代码,例如:

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

以下是PHP代码段:

`<?php
//Create connection credentials
$db_host = 'localhost';
$db_name = 'quizzer';
$db_user = 'root';
$db_pass = '';

$mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name);

if($mysqli->connect_error){
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

//Set question number
$number = (int) $_GET['n'];

/*
*   Get total questions
*/
$query = "SELECT * FROM `questions`";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;

/*
*   Get Question
*/
$query = "SELECT * FROM `questions`
            WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$question = $result->fetch_assoc();

/*
*   Get Choices
*/
$query = "SELECT * FROM `choices`
            WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);


<?php $question['question_number']; ?> of <?php echo $total; ?>

<?php echo $question['text']; ?>

<?php while($row = $choices->fetch_assoc()): ?>

    <?php echo $row['id']; ?>" /><?php echo $row['text']; ?>

<?php endwhile; ?>`

2 个答案:

答案 0 :(得分:0)

(从评论部分继续)
连接到mysql之后:-

$mysqli->set_charset("utf8mb4");

这会将您的默认字符集设置为“ utfmb4”,因此此后的每个查询都将保留此字符集。

要使其成为“永久”,请在my.cnf中:

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

答案 1 :(得分:0)

只需添加mysqli_set_charset($ mysqli,“ utf8”);

$mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name);

mysqli_set_charset($mysqli,"utf8");